Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

981
Views
¿Hay alguna manera de revertir el formato de Intl.NumberFormat en JavaScript?

El Intl.NumberFormat (ver el documento de Mozilla ) proporciona una buena manera en Javascript para formatear números en una versión local actual como esta:

 new Intl.NumberFormat().format(3400); // returns "3.400" for German locale

Pero no pude encontrar una manera de revertir este formato. ¿Hay algo como

 new Intl.NumberFormat().unformat("3.400"); // returns 3400 for German locale

Gracias por cualquier ayuda.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

He encontrado una solución alternativa:

 /** * Parse a localized number to a float. * @param {string} stringNumber - the localized number * @param {string} locale - [optional] the locale that the number is represented in. Omit this parameter to use the current locale. */ function parseLocaleNumber(stringNumber, locale) { var thousandSeparator = Intl.NumberFormat(locale).format(11111).replace(/\p{Number}/gu, ''); var decimalSeparator = Intl.NumberFormat(locale).format(1.1).replace(/\p{Number}/gu, ''); return parseFloat(stringNumber .replace(new RegExp('\\' + thousandSeparator, 'g'), '') .replace(new RegExp('\\' + decimalSeparator), '.') ); }

Usándolo así:

 parseLocaleNumber('3.400,5', 'de'); parseLocaleNumber('3.400,5'); // or if you have German locale settings // results in: 3400.5

No es la mejor solución, pero funciona :-)

Si alguien conoce una mejor manera de lograr esto, no dude en publicar su respuesta.

Actualizar

  • Envuelto en una función reutilizable completa
  • Usando la clase regex \p{Number} para extraer el separador. Para que también funcione con dígitos no arábigos.
  • Uso de números con 5 lugares para admitir idiomas en los que los números se separan cada cuatro dígitos.
over 4 years ago · Santiago Trujillo Report

0

Aquí he creado una función para la función Reverse of format() . Esta función admitirá el formato inverso en todas las configuraciones regionales.

 function reverseFormatNumber(val,locale){ var group = new Intl.NumberFormat(locale).format(1111).replace(/1/g, ''); var decimal = new Intl.NumberFormat(locale).format(1.1).replace(/1/g, ''); var reversedVal = val.replace(new RegExp('\\' + group, 'g'), ''); reversedVal = reversedVal.replace(new RegExp('\\' + decimal, 'g'), '.'); return Number.isNaN(reversedVal)?0:reversedVal; } console.log(reverseFormatNumber('1,234.56','en')); console.log(reverseFormatNumber('1.234,56','de'));

over 4 years ago · Santiago Trujillo Report

0

Lo acabo de resolver usando reemplazos de grupo.

 const exp = /^\w{0,3}\W?\s?(\d+)[.,](\d+)?,?(\d+)?$/g const replacer = (f, group1, group2, group3) => { return group3 ? `${group1}${group2}.${group3}` : `${group1}.${group2}` } const usd = '$10.15'.replace(exp, replacer) // 10.15 const eu = '€01.25'.replace(exp, replacer) // 1.25 const brl = 'R$ 14.000,32'.replace(exp, replacer) // 14000.32 const tai = 'TAI 50.230,32'.replace(exp, replacer) // 50230.32 // just to test! const el = document.getElementById('output') const reverseUSD = new Intl.NumberFormat('en-us', { style: 'currency', currency: 'USD' }).format(usd) el.innerHTML += `<br> from: ${reverseUSD} to ${parseFloat(usd)}` const reverseBRL = new Intl.NumberFormat('pt-br', { style: 'currency', currency: 'BRL' }).format(brl) el.innerHTML += `<br> from: ${reverseBRL} to ${parseFloat(brl)}` const reverseTAI = new Intl.NumberFormat('en-us', { style: 'currency', currency: 'TAI' }).format(tai) el.innerHTML += `<br> from: ${reverseTAI} to ${parseFloat(tai)}` const reverseEU = new Intl.NumberFormat('eur', { style: 'currency', currency: 'EUR' }).format(eu) el.innerHTML += `<br> from: ${reverseEU} to ${parseFloat(eu)}`
 <output id=output></output>

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!