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

292
Views
No quiero formatear una cadena de números como moneda en javascript pero no sé cómo formatear mi cadena

entonces lo que quiero decir es:

1000 debería convertirse en 10,00
10000 debería convertirse en 100,00
100000 debería convertirse en 1000,00

¿Puede alguien ayudarme?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString#using_options

 var number = 1000; // request a currency format console.log(number.toLocaleString('en-US', { style: 'currency', currency: 'USD' }));

Sin métodos y solo formateando con comas.

 function addComma(value) { return value.split('').map((v, i) => { if ( i === value.length - 2) { return ',' + v } return v }).join('') } console.log(addComma('1000')) console.log(addComma('10000')) console.log(addComma('100000'))
about 4 years ago · Juan Pablo Isaza Report

0

https://www.delftstack.com/howto/javascript/javascript-currency-format/

puede transformar su cadena en número y luego usar toLocaleString()

aquí hay un ejemplo de la página web

 const number = 2000; number.toLocaleString('en-IN', {style: 'currency',currency: 'INR', minimumFractionDigits: 2}) console.log(number);
about 4 years ago · Juan Pablo Isaza Report

0

Otra solución, simplemente dividiendo la cadena en enteros y centavos :

 function addComma(n) { if (n.length == 0) { return '0,00'; } if (n.length == 1) { return '0,0' + n; } if (n.length == 2) { return '0,' + n; } let whole = n.substring(0, n.length-2); let cents = n.substring(n.length-2); return whole + ',' + cents; }
about 4 years ago · Juan Pablo Isaza 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!