0

ngx-mask specific question!

Is there a way to allow user to input comma and dot as a decimal marker, but keep the dot in displayed value and then trim thousand separators?

User inputs "123456,78", sees "123 456.78", value is "123456,78"
User inputs "123456.78", sees "123 345.78", value is "123456.78"

Cant find any way to achieve this result with ngx-mask

Thanks in advance!

Update: seems stackoverflow needs some debugging but I have zero clue what info is needed cuz I just dont see any way I can achieve this functionality. Ok, I'll show what I tried.

I tried custom patterns, like this:

customPatterns = {
    D: {
      pattern: new RegExp('[,.]'),
      symbol: '.',
    },
    0: { pattern: new RegExp('\\d') },
  };

...

[mask]='0*D00'

But symbol doesnt work for some reason, comma stays displayed if you input it

And I tried something like [mask]="'0*(,|.)00'", but it doesnt work at all

Update: here is my final imask config:

  {
    mask: Number,
    scale: 2,
    signed: true,
    thousandsSeparator: ' ',
    padFractionalZeros: true,
    normalizeZeros: true,
    radix: '.',
    mapToRadix: [',', '/']
  }
lucifer63
  • 784
  • 9
  • 32
  • I'm a little confused about what you expect the user experience to be. If i type `1,1` on the text box, should the comma become a `.` as i type? – Roddy of the Frozen Peas Nov 02 '21 at 23:46
  • @RoddyoftheFrozenPeas, yeah, you got it right – lucifer63 Nov 03 '21 at 00:03
  • To solve the problem I just switched to imask, there is no such trouble in imask. But the question is still open – lucifer63 Dec 16 '21 at 19:24
  • how did u solve using imask? Pattern masks doesnt work for me @lucifer63 – Renê Guilherme Nucci Jun 23 '22 at 21:19
  • 1
    @RenêGuilhermeNucci, hello! With mapToRadix you can ask Imask to treat specified symbols as radix symbol - you enter one of these symbols and it is substituted with radix symbol. Here is my config: { mask: Number, scale: 2, signed: true, thousandsSeparator: ' ', padFractionalZeros: true, normalizeZeros: true, radix: '.', mapToRadix: [',', '/', 'б', 'ю'] } – lucifer63 Jun 25 '22 at 16:06
  • @lucifer63 excellent, I didn't see that, ty vm! – Renê Guilherme Nucci Jun 27 '22 at 12:09

1 Answers1

0

answer : In React, how to format a number with commas?

number.toLocaleString(); // "1,234,567,890"

// A more complex example: 
var number2 = 1234.56789; // floating point example
number2.toLocaleString(undefined, {maximumFractionDigits:2}) // "1,234.57"