Format number to BRL

Benchmark created on


Description

The function should receive a number and return a string on BRL format.

Setup

const number = 123456.789

Test runner

Ready to run.

Testing in
TestOps/sec
Intl number format
const result = new Intl.NumberFormat('pt-BR', { style: 'currency', currency: 'BRL' }).format( number)
ready
to localeString
const result = Number(number).toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' })
ready
original
const formattedValue = number.toFixed(2)
  const [integerPart, decimalPart] = formattedValue.split('.')
  const integerPartWithThousandSeparators = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, '.')

const result = `R$ ${integerPartWithThousandSeparators},${decimalPart}`
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.