Skip to content

Problem of Reversed Number Formatting with Thousand Separators for BRL Currency #124

Answered by appcarrareto
appcarrareto asked this question in Q&A
Discussion options

You must be logged in to vote

Following the example, I set it up for BRL currency, it worked correctly with the initial value, but when I clear the input, the return gets misconfigured and I can't type again, I am not knowing how to set it up.

const options = {
 preProcess: val => val.replace(/[R$,]/g, ''),
  postProcess: val => {
    if (!val) return ''

    const sub = 3 - (val.includes('.') ? val.length - val.indexOf('.') : 0)

    return Intl.NumberFormat('pt-BR', { style: 'currency', currency: 'BRL' })
      .formatToParts(val)
      .map(v => v.value)
      .join('')
      .slice(0, sub ? -sub : undefined)
  }
}

<input
  v-maska:[options]
  data-maska="0.99"
  data-maska-tokens="0:\d:multiple|9:\d:optional"
  d…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@beholdr
Comment options

Answer selected by appcarrareto
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants