Help to get decimal value of mask input (Ionic 3 + br Masker)

Asked

Viewed 222 times

3

I am having a hard time with a problem that I believe is very easy to solve, but I am struggling hard enough to reach such a solution. I have a simple input that format using the brmasker library (ionic3). The user enters a value and it is formatted in real time, so far so good. In my console.log I receive the format value properly in string format. But when it comes to going up to the database this value, I need it to be NUMBER, so I use a parseFloat() as I always have, but I can’t get the values correctly, it doesn’t consider the two decimal places after the comma and if I try to use a parseFloat(2) it returns me a string, not a number.

Follow my code and some prints explaining the problem.

<ion-item>
  <ion-input type="text" [(ngModel)]="valorMeta"
  [brmasker]="{money: true, thousand: '.',  decimalCaracter: ',', decimal: '2'}">
  </ion-input>
</ion-item>

No ts is something simple like:

console.log(this.valorMeta);
console.log(parseFloat(this.valorMeta));

The images:

inserir a descrição da imagem aquiinserir a descrição da imagem aquiinserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • I managed to get a slightly better result, using this condition: console.log(parseFloat(this.valorMeta.replace(',', '.'))); But it still doesn’t return as expected, when the numbers are too high, it still doesn’t catch correctly.

1 answer

1


You need to treat your string. Remove the dots and replace the comma with a dot.

const n = '2.368,67',
nG = '256.695.693,25';

console.log(n.split('.').join('').replace(',','.'));

console.log(nG.split('.').join('').replace(',','.'));

  • 1

    Thanks man, solved this way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.