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
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
Simply pass it to the Number function:
var num = Number(str);
If I use Float.parseFloat("1.4E-46"), it returns 0.0 instead of negative infinite. In the case of positive bigger number, it returns infinite.
The number 1.4E-46 is not a negative number. It is a very small positive number.
The opposite of 1.4E+46 is -1.4E+46. So if you do Float.valueOf("-1.4E+46") you will get -Infinity.
That is not a negative number; it is an extremely small positive number. This is "computerized" scientific notation. 1.4E-46 is equivalent to 1.4 x 10-46.
The Float.parseFloat method parses the number the same as Float.valueOf(String), whose javadocs state:
if the exact value of s is small enough in magnitude (less than or equal to MIN_VALUE/2), rounding to float will result in a zero.
Java's parsing method determined that the value is less than Float.MIN_VALUE / 2, so it returned a 0.0.
There should be nothing special to parsing negative numbers compared to positive number.
float f = Float.parseFloat("-1.0");
The above code should work perfectly fine.
What might be wrong with your code, is that you're trying to parse a float with the wrong decimal separator. If your locale has . as decimal separator, the above code is OK. If however your locale has , as the decimal separator, the parsing will fail (with a NumberFormatException).
So make sure you're splitting the original correctly, and that each of the parts after the split are on a valid format (e.g. with the correct decimal separator).
Update:
If you want to know how to parse a number using a specific locale, you could for instance look at this question.
I had a similar problem today and the problem was that the minus sign in the input string was actually an m-dash character. That was nasty! So that's definitely worth checking.
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
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.
JIT issue perhaps? I found the implementation of Math.abs in the JavascriptCore source which I'm guessing is different from Nitro (ios js engine) or is perhaps marked as JIT executable.
Value MathFuncImp::call(ExecState *exec, Object &/*thisObj*/, const List &args)
{
double arg = args[0].toNumber(exec);
double arg2 = args[1].toNumber(exec);
double result;
switch (id) {
case MathObjectImp::Abs:
result = ( arg < 0 || arg == -0) ? (-arg) : arg;
break;
This can be implemented directly in javascript. So you can replace your Math.abs calls with
result = ( arg < 0 || arg == -0) ? (-arg) : arg;
This will resolve the issue and be cross browser compatible.
Submitted Apple Bug: 16209709
Does anyone know how this kind of bug occurs or a better way to describe it?
Bit Flip? Memory Collision? Overflow of some sort?
I suggest that you should use it in standard.
for parseInt, it is better to pass second parameter like:
parseInt(1, 10). Or in some IOS system, it will give you wrong answers.
For example, parseInt(8) will be traded as octal in IOS (it is weird..).
It should work fine, the problem is that you're using the wrong character, an ndash – vs a hyphen -:
var x = parseInt("-2147483648");
console.log(x);
If you copy/paste that you'll see that it works now.
After 2 days of trying various approaches, this solved my problem. The numeric picker was using wrong negative format. I changed the field to textstring and then with parseInt I got correct negative numbers.