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

200
Views
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 answers
Answer question

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 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!