Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trim to 2 decimals

Tags:

javascript

I have:

onclick="document.getElementById('field1').value = 
Math.round((parseFloat(document.getElementById('field2').value,2)*100))/100 + 
Math.round((parseFloat(document.getElementById('field3').value,2)*100))/100;"

Most numbers round ok to 2 decimal points which is what I need.

However, with an example like

onclick="document.getElementById('field1').value = 
Math.round((parseFloat(document.getElementById('21.29').value,2)*100))/100 + 
Math.round((parseFloat(document.getElementById('54.70').value,2)*100))/100;"  

Field 1 is returning 75.99000000000001 How can I trim to 75.99 consistently?

like image 426
user821522 Avatar asked Sep 05 '25 17:09

user821522


1 Answers

var num = 5 / 6;

var display = num.toFixed(2)

num outputs: 0.8333333333333334

display outputs: "0.83"
like image 69
Bobby Borszich Avatar answered Sep 07 '25 08:09

Bobby Borszich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!