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

155
Views
parseFloat whole number with two decimal points -Javascript

Below i have a use case, where a number is stored in db as decimal(18, 2). When trying to read the data, the numbers with .00 are converted to integer.

Using .toFixed(2) changes the format from number to string. // as expected.

How can i retain the format.

Input

    const x1 = 4000.00;
    const x2 = 4000.01;

    const y1 = parseFloat((x1).toFixed(2)); // actual: 4000 expected: 4000.00
    const y2 = parseFloat((x2).toFixed(2)); // actual: 4000.01 expected: 4000.01

    // const y1 = parseFloat((x1).toFixed(2)).toFixed(2); //datatype: actual: string, expected: number 
    // const y2 = parseFloat((x2).toFixed(2)).toFixed(2); //datatype: actual: string, expected: number

    console.log(typeof(y1), y1);
    console.log(typeof(y2), y2);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

decimal(18,2) is a fixed-precision number format, here meaning "18 decimal digit, with 2 of them to the right of the decimal point". Typically stored as either packed decimal or an integer. with precision and scale maintained by the programming language.

Javascript numbers are not like that. They are IEEE 754 double-precision binary floating point numbers: they have a floating decimal (binary) point, and trade precision for range.

You need to read What Every Programmer Should Know About Floating-Point Arithmetic, or Why don’t my numbers add up?.

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!