Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get a float using parseFloat(0.00)

How can I get a float of 0.00. The reason I need 0.00, is because I am going to be accumalating float values by adding. Hence starting with 0.00. I tried

 var tmp ='0.00'
 tmp  = parseFloat(tmp.toString()).toFixed(2);
 totals = parseFloat(tmp)

tmp is 0.00 but totals is 0. How can I make total 0.00? I need it to stay as a float and not a string. Thanks

like image 423
Mary Avatar asked Feb 12 '23 16:02

Mary


1 Answers

You can use the string tmp variable and then when you need to add to it use:

tmp = (+tmp + 8).toFixed(2);

JSFIDDLE DEMO

Or simply write a function to do that seeing that you'll have to do that many times:

function strAdd( tmp, num ) {
    return (+tmp + num).toFixed(2);
}
like image 139
PeterKA Avatar answered Feb 15 '23 04:02

PeterKA