Input number with at least two decimal places

How I can mask the ionic input, type=number, to accept only number with 2 two decimal places?

<ion-input text-right [(ngModel)]=“mynumber” type=“number” >

I tried step=“0.01” but it not works!

I’ve created this solution to myself, I hope it helps you too:

<ion-input type="number" value="0.00" (ionChange)="formataNumero($event)"></ion-input>

And:

  formataNumero(e: any, separador: string = '.', decimais: number = 2) {
    let a:any = e.value.split('');
    let ns:string = '';
    a.forEach((c:any) => { if (!isNaN(c)) ns = ns + c; });
    ns = parseInt(ns).toString();
    if (ns.length < (decimais+1)) { ns = ('0'.repeat(decimais+1) + ns); ns = ns.slice((decimais+1)*-1); }
    let ans = ns.split('');
    let r = '';
    for (let i=0; i < ans.length; i++) if (i == ans.length - decimais) r = r + separador + ans[i]; else r = r + ans[i];
    e.value = r;
  }

Just type the numbers. Remembering that your end user will use only the number digits and “backspace”, no more. If you set “decimais” to 3, you can work with weights or something like that. Test into DevApp too.

Simple like that.

6 Likes

Somebody marks this as the right answer for God’s sake! Congratulations, man. Very good job! People request installation of angular2-text-mask to do this trick. Your solution is simpler and better.

Thanks! We need to simplify our lives helping ourselves!