0

I'm using the following command to parseFloat a number from a div (which was created by php by getting it from a database)

var price = parseFloat($('#priceofproduct').text().replace(',', '.'));

Now, most of the time it results in €5,99 for example. But sometimes it also results in €5,9900000009 as this is the way it was inserted in the database somehow. How can I set up a limit to only display a maximum of 4 digits (like €39,99)?

B. S.
  • 95
  • 1
  • 3
  • 9
  • post some codes what you've tried so far – polin Nov 28 '12 at 09:39
  • 3
    Hopefully helpful: [Precise Financial Calculation in JavaScript. What Are the Gotchas?](http://stackoverflow.com/questions/2876536/precise-financial-calculation-in-javascript-what-are-the-gotchas). For displaying purposes, using floats is probably fine, but for any computations, don't use them. – Felix Kling Nov 28 '12 at 09:39
  • _"a maximum of 4 digits (like €39,99)"_ - I think you want a maximum of two decimal places rather than four digits, otherwise you might get something like `€3,999`. For a quick fix use `parseFloat(someString).toFixed(2)`, but then read the topic Felix linked to. – nnnnnn Nov 28 '12 at 09:42

0 Answers0