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

121
Views
Always hide .00 decimal number

I would like to format my decimal numbers to display 2 decimal places but .00 will always hide.

example

value      expect     return     
------     -------    ------
1.00       1           1.00
1          1           1.00
1.6        1.60        1.60
1.451      1.45        1.45



My Code

Vue.filter("currency", value => {
    return parseFloat(value).toFixed(2);
});
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Remove .00 from the end if it exists:

 return parseFloat(value).toFixed(2).replace(/\.00$/, '')
about 4 years ago · Juan Pablo Isaza Report

0

You can simply put the toFixed INTO the parseFloat like this:

Vue.filter("currency", value => {
    return parseFloat(value.toFixed(2))
});

toFixed will round/pad the number to a specific length, but also convert it to a string. Converting that back to a numeric type will not only make the number safer to use arithmetically, but also automatically drop any trailing 0's.

Reference: https://newbedev.com/remove-insignificant-trailing-zeros-from-a-number

Update

This solution doesn't consider the case: 1.6 -> 1.60 (check jsfiddle in the comment)

about 4 years ago · Juan Pablo Isaza Report

0

Just check whether the number is wholenumber by checking the reminder on division with 1

Vue.filter("currency", value => {
    return parseFloat(value) % 1 === 0 ? parseFloat(value).toString() : parseFloat(value).toFixed(2);
});

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!