The parseInt function can be used to parse strings to integers and uses this format: parseInt(string, radix);

Ex: parseInt("-10.465", 10); returns -10

To parse floating point numbers, you use parseFloat, formatted like parseFloat(string)

Ex: parseFloat("-10.465"); returns -10.465

Answer from Grice on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › parseFloat
parseFloat() - JavaScript | MDN
The parseFloat function converts its first argument to a string, parses that string as a decimal number literal, then returns a number or NaN.
🌐
GitHub
github.com › globalizejs › globalize › issues › 44
parseInt and parseFloat fail (NaN) with negative currency · Issue #44 · globalizejs/globalize
January 7, 2011 - // Actual Globalize.parseInt( Globalize.format(-3.91, "c") ); // NaN Globalize.parseFloat( Globalize.format(-3.91, "c") ); // NaN · The expected values are returned if the original value is positive rather than negative. No one assigned · bug · No type · No projects ·
Published   Jun 22, 2011
🌐
Learnjavascript
learnjavascript.co.uk › reference › globals › parsefloat.html
JavaScript parseFloat() Function
Parses a string argument (converting it to a string first if required) and attempts to return a floating point number. Below are some examples of using the parseFloat() function. // Parsing string to floats. var parsedValues = new Array(6); parsedValues[0] = parseFloat('17'); // integer ...
🌐
Scaler
scaler.com › home › topics › javascript parsefloat()
JavaScript parseFloat() Method - Scaler Topics
March 7, 2024 - In such a case, parseFloat() just converts the integer string to an integer number. When there is a leading minus or plus sign · Explanation: When there is a minus sign before a numeric character, parseFloat() interprets it to be a negative number.
🌐
Pentaho
forums.pentaho.com › threads › 155539-Javascript-Convert-string-to-number-(with-a-negative-sign)
Javascript - Convert string to number (with a negative sign)
I tried using the calculator to create a copy of field A and automatically cast it to a number but it fail when there's negative values (positive works though). So now I come back to my JavaScript step and my guess is that I certainly can cast this string into a number directly in there but can't figure out :-( Anyone can help on this matter please? Thank you for your time. Regards, Martin ... Is it parseFloat() you are looking for?
Find elsewhere
🌐
Coderwall
coderwall.com › p › 5tlhmw › converting-strings-to-number-in-javascript-pitfalls
Converting Strings to Number in Javascript: Pitfalls (Example)
September 27, 2024 - It's all good if you never handle hexadecimal numbers; for instance: parseInt(-0xFF) // returns -255 parseInt("-0xFF") // returns -255 parseFloat(-0xFF) // returns -255 parseFloat("-0xFF") // returns 0 · (Note, a negative hexadecimal number in a string is a special case that will go funky town in your application if you are parsing it.
🌐
CopyProgramming
copyprogramming.com › howto › javascript-js-parse-float-negative-returns-nan
Negative values passed to parseFloat() in JavaScript result in NaN - Javascript
April 18, 2023 - When a character that is not a plus sign (+), minus sign (- U+002D HYPHEN-MINUS), numeral (0-9), decimal point (.), or exponent (e or E) is encountered by parseFloat, it disregards the invalid character and any characters that follow it, and returns the value up to that character.
🌐
MSR
rajamsr.com › home › javascript parsefloat(): how to avoid common mistakes
JavaScript ParseFloat(): How to Avoid Common Mistakes | MSR - Web Dev Simplified
January 28, 2024 - The converted result will retain the negative sign. However, for numbers with a + sign, the parseFloat() function will ignore the sign in the converted number.
🌐
GitHub
github.com › golang › go › issues › 34945
strconv: ParseFloat not working correctly for negative exponent · Issue #34945 · golang/go
Generally, strconv.ParseFloat will return the float32/float64 value closest to the decimal representation - this invariably involves rounding.
Published   Oct 17, 2019
Author   Dabada
🌐
Microsoft Learn
learn.microsoft.com › en-us › archive › msdn-technet-forums › ebb5d342-f0c9-4bb3-ae75-9e1f7c00857c
Javascript + replace parenthesis with - sign for negative numbers to be used by parseFloat | Microsoft Learn
-1 :1); // dsc break; } //Return your value return retVal; } function cleanUpCurrency(s){ var expression = /^\$?\(?[\d,\.]*\)?$/; //Check if it is in the proper format if(s.match(expression)){ //It matched - strip out parentheses and append - at front return parseFloat('-' + s.replace(/[\$\(\),]/g,'')); } else{ return parseFloat(s); } } ... You could likely write a very basic function to determine if your value was a negative value through a Regular Expression such as : //Matches anything in the form : $(number) ^\$\([\d,\.]*\)$
🌐
GitHub
github.com › ziglang › zig › issues › 17662
right shift broken on negative numbers in comptime (aka parseFloat is result is doubled for f32 when run at comptime) · Issue #17662 · ziglang/zig
October 22, 2023 - I've noticed if I comment out this chunk of code from parseFloat it works as expected. if (T == f16 or T == f32 or T == f64) { // If significant digits were truncated, then we can have rounding error // only if `mantissa + 1` produces a different result. We also avoid // redundantly using the Eisel-Lemire algorithm if it was unable to // correctly round on the first pass. if (convertEiselLemire(T, n.exponent, n.mantissa)) |bf| { if (!n.many_digits) { return bf.toFloat(T, n.negative); } if (convertEiselLemire(T, n.exponent, n.mantissa + 1)) |bf2| { if (bf.eql(bf2)) { return bf.toFloat(T, n.negative); } } } }
Published   Oct 22, 2023
Top answer
1 of 4
3

Nothing strange here, this is how IEEE floating point works.

Since what you seem to be doing here is formatting a currency value, I would suggest writing a reusable function to do that so it's done consistently throughout your app.

There are several approaches, however which to use depends on the application

It's not a bug. There is nothing wrong with -0 as a value in IEEE floating point: -0 === 0, x + -0 = x, x * -0 = -0 etc. In arithmetic minus 0 works exactly the same way 0 does.

Usually I would handle the -0.00 when formatting, since only -0.00 "looks wierd", -10.20 is just an overdraft :)

res = res.toFixed(2);
if(res === '-0.00'){
  res = '0.00';
}

I suggest this article to understand IEEE floating point better: What Every Computer Scientist Should Know About Floating-Point Arithmetic

2 of 4
0

javascript and machine in total handle with double as almost infinite number.

when rounding to 2 decimal places, javascript gives the nearest rounded number.

to fix it, use:

var pay = -0.33;
var res = parseFloat(pay) + parseFloat(0.11) + parseFloat(0.22);
res = res.toFixed(2);
return (Math.round(res * 100) / 100);

way better than other's workarounds here will evaluating strings..

EDIT

if you change the round number, better use this:

var fixRound = 2,
    pay = -0.33,
    rounding = Math.pow(10, fixRound),
    res = parseFloat(pay) + parseFloat(0.11) + parseFloat(0.22);
    res = res.toFixed(fixRound );
    return (Math.round(res * rounding ) / rounding );

this way, all you need to change in the code in order for it to work with toFixed(3), toFixed(4),toFixed(5) and so on, is just set fixRound value to be the number you wish.

🌐
GitHub
github.com › redux-form › redux-form › issues › 1470
[v5] input fields of type number not properly supporting negative values? · Issue #1470 · redux-form/redux-form
June 21, 2016 - After some poking around, I found that editing node_modules/redux-form/lib/events/getValue.js and removing the parseFloat() call seems to work: if (type === 'number' || type === 'range') { // parseFloat(value); return value; }
Published   Aug 05, 2016