3

Hopefully someone can help with this. I am simply looking to add the final zero to the decimal part of a price in a price calculator I am written.

The calculator starts with a user selecting a standard "Sign Up" fee of €25.50 and then selecting other packages to increase the price.

At the moment, the standard price displays as €25.5.

costs = jQuery('.cost-container').text();
total = Number(costs) + (25.50).toFixed(2);

I got this code by looking at the following similar query: How to add a trailing zero to a price with jQuery

Currently, if I enter 25.511 as my value, the result displays as 25.51, which shows that the code is sort of working, my issue is just when the second decimal place is 0 that it does not display.

Can anybody spot what is wrong?

Cheers

Damien

Community
  • 1
  • 1
damienoneill2001
  • 498
  • 7
  • 17
  • Did you try a `.append(0)` to the final price? – Parrotmaster Mar 24 '14 at 12:29
  • 4
    Add this line: `total = total.toFixed(2);` http://jsfiddle.net/DG69v/ – RobinvdA Mar 24 '14 at 12:30
  • Thanks guys for your answers. @RobinvdA, whilst that seemed to work when I viewed it in the Console, the actual result still displayed as 25.5. I had a look further down at some more of the code for the rest of the calculation and tried `costs = costs.toFixed(2)` and that solved my problem! All working now, thank you so much! – damienoneill2001 Mar 24 '14 at 12:52

1 Answers1

1

Try

costs = jQuery('.cost-container').text();
total = ( Number(costs) + 25.50 ).toFixed(2);
Chris Gunawardena
  • 6,246
  • 1
  • 29
  • 45
  • Be careful with the use of "toFixed" cause it returns a string, use .toFixed(2)/1 to get a number – frikinside Mar 24 '14 at 12:39
  • @frikinside After looking at the console, I did notice that the number was appearing as a string like you suggested, however adding a variation of `total = total.toFixed(2);` from @RobinvdA fixed the issue – damienoneill2001 Mar 24 '14 at 12:55