javascript parseFloat ‘500,000’ returns 500 when I need 500000
parseFloat( theString.replace(/,/g,”) );
parseFloat( theString.replace(/,/g,”) );
Use toFixed() to round num to 2 decimal digits using the traditional rounding method. It will round 4.050000000000001 to 4.05. num.toFixed(2); You might prefer using toPrecision(), which will strip any resulting trailing zeros. Example: 1.35+1.35+1.35 => 4.050000000000001 (1.35+1.35+1.35).toFixed(2) => 4.05 (1.35+1.35+1.35).toPrecision(3) => 4.05 // or… (1.35+1.35+1.35).toFixed(4) => 4.0500 (1.35+1.35+1.35).toPrecision(4) => 4.05 Reference: JavaScript Number Format … Read more
Update 2 New to ECMAScript 6 is the Object.is() function. This is designed to be a further enhancement of the === check. One of the benefits of this new function is that Object.is(NaN, NaN) will now return true. If you’re able to utilize ECMAScript 6, then this would be the most readable and consistent solution … Read more
This is “By Design”. The parseFloat function will only consider the parts of the string up until in reaches a non +, -, number, exponent or decimal point. Once it sees the comma it stops looking and only considers the “75” portion. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseFloat To fix this convert the commas to decimal points. var fullcost = … Read more