Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

199
Vistas
Convert money value to cents

I want to get string input for money value and I need integer (cents) to store it. But I need to present it back to the user on UI which mean that I need to get that value back from integer.

The problem is 455.000 (455 x 1000) but I get 455. So I made something terribly wrong in this calculation.

const toCent = amount => {
  const str = amount.toString().replace(',', '.')
  const [int] = str.split('.')
  return Number(amount.toFixed(2).replace('.', '').padEnd(int.length === 1 ? 3 : 4, '0'))
}
console.log(toCent(parseFloat('45.99')));
console.log(toCent(parseFloat('455.000')));
console.log((Math.round((toCent(parseFloat('45.99')) / 100 + Number.EPSILON) * 100) / 100).toString());
console.log((Math.round((toCent(parseFloat('455.000')) / 100 + Number.EPSILON) * 100) / 100).toString());

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The following snippet will work with input of the form D[sC] where D is dollars with optional thousand separators, s is the separator for dollars and cents, and C is two-digit cents (zero-padded).

 const toCent = amount => { const str = amount.replace(',', '.') return str.length < 3 || str[str.length - 3] == '.' ? Number(str.replace('.', '')) : Number(str.replace('.', ''))*100 } console.log(toCent('45.99')); console.log(toCent('455.000')); console.log(toCent('45.99')/100); console.log(toCent('455.000')/100);

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda