-1

I am aware this question has been asked before and i have read up on the subject, but still the solution isn't clear to me. And my attempts fails.

<div class="subtotal" data-content="€">30.50</div>

but with the following jquery js it reads 30.509

test = parseFloat($('.subtotal').text());//reads 30.509

i tried

test = parseFloat(Number($('.subtotal').text()).toFixed(2));//gives NaN

How do i make sure javascript reads the subtotal as 30.50 and not 30.509?

Regards,

alex
  • 4,804
  • 14
  • 51
  • 86
  • 3
    Cannot reproduce your example, there's probably something else happening to your numbers. Most likely there's a plugin that does something since you've specified the attribute `data-content`. It works as it should, http://jsbin.com/podefeheri/1/ – Henrik Andersson May 18 '15 at 11:09
  • Have you taken a look at this before? http://stackoverflow.com/questions/4640404/parsefloat-rounding – Zim84 May 18 '15 at 11:12
  • as stated by @limelights, it doesn't read a different value. For rounding up `parseFloat($('.subtotal').text()).toFixed("2")` – Sami May 18 '15 at 11:15
  • @Zim84 i am trying toPrecision but `test = parseFloat($('.subtotal').text()).toPrecision(5) logs 30.509` and `test = parseFloat($('.subtotal').text()).toPrecision(4) logs 30.51` where i expected it to log 30.50 – alex May 18 '15 at 11:39

2 Answers2

1

It doesn't read a different value. The difference comes because of the precision of how the floating point numbers are stored in memory. As you store a number, you don't need to worry about that. You'll never be able to store it exactly as you want.

When displaying the number you'll have to trim it to two decimals, which should correctly display it as it was read. Your problem is just about how the number is displayed.

Dan Bizdadea
  • 1,292
  • 8
  • 15
  • i think your suggestion is indeed what is happening. But i don't get how i can display the number correctly. I thought toFixed(2) was sufficient as i can't use Math.round, which will round to 31 – alex May 18 '15 at 11:28
  • You can try : Math.round(num * 100) / 100. But also, toFixed() should be working fine. – Dan Bizdadea May 18 '15 at 11:37
  • hmm, i am testing with other hardcoded variables like 34 `
    34
    ` and javascript `test = parseFloat($('.subtotal').text()); console.log('test '+test );` and now test reads 349 so i guess 9 is being added, but how and why???
    – alex May 18 '15 at 11:50
  • do you have other elements with the clss .subtotal ? – Dan Bizdadea May 18 '15 at 11:52
  • tx dan, i will check the rest of the js. – alex May 18 '15 at 11:58
  • Wow, thx to you i have found the problem. In the rest of the html code there was another div with class='subtotal'. The first numeric value was also being used. – alex May 18 '15 at 12:07
0

I see redundancy, why parseFloat a value that's already a Number?

Just parseFloat the text and should get the work done.

var str_subtotal = "12.348";
var num_subtotal = parseFloat(str_subtotal); // 12.348 (as number)
console.log(num_subtotal.toFixed(2));

Notice that toFixed WILL NOT round the number, use Math.round instead

josegomezr
  • 888
  • 4
  • 15