Javascript convert float to Exponential [duplicate] - javascript

This question already has answers here:
How can I convert numbers into scientific notation?
(3 answers)
Closed 9 years ago.
I have to convert the float to exponential..
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000021
Using
parseFloat(result)
Gives 2.1e-87
But
10000000
Gives 10000000 (same) but I like to get 100.0e+3 etc..So I used parseFloat(result).toExponential(3); But the problem here is it is truncating everything ev even if it has more values for example 111222333 it makes it as 111.22+3 so while I reconverting as integer I am cannot get the original value..
Is there any javascript function to achieve this or how can I achieve this..
Thanks in advance..

If you use toExponential() with out the parameter, it will not drop the fractions:
console.log( (111222333).toExponential() ); // "1.11222333e+8"
The parameter specifies the amount of digits after the decimal point. If you drop it, it displays as many digits as needed (wrt to the double precision JavaScript uses internally).

Related

String conversion to integer [duplicate]

This question already has answers here:
How to convert a String to long in javascript?
(2 answers)
Closed 4 years ago.
I am trying to convert this "9876543210223023" into integer which is received as a string .I have used parseInt but it gets converted into 9876543210223024.
This is causing failure in my validations .
Please suggest how will the value remain same after conversion.
9876543210223023 is > than 9007199254740991, which is Number.MAX_SAFE_INTEGER.
JavaScript has a Number type, which internally is a 64 bit floating point number.
If the range is too big, the machine episilon is too large, and the number is rounded to the nearest representable number, in this case 9876543210223024.
You need a biginteger library, if you want to process numbers of this size.
If your browser is modern enough (aka Chrome/Chromium), it might have the type "BigInteger" already built-in.
In that case: BigInt("9876543210223023", 10)
Otherwise, the linked BigInt-library will act as polyfill.

not rounding tofixed [duplicate]

This question already has answers here:
Truncate number to two decimal places without rounding
(43 answers)
Closed 5 years ago.
I'm trying to get the first decimals of a float number without any kind of rounding.
Example:
var myfloat = 1.1864526;
myfloat = myfloat.toFixed(2);
It returns 1.19 but I need 1.18.
I'm pretty sure there is an easy solution but I am unable to find it without converting the number to a string (not useful in this case).
Multiply the float value by 100, get the int value of the result then divide that int by 100. Something like this should work:
((int)(myFloat*100)) / 100

Javascript : Math.round is not working correctly for specific digits [duplicate]

This question already has answers here:
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 5 years ago.
I am facing weird issue during rounding the value using javascript. Some of the value is not rounding in correct format.
var n =17.955 ;
var roundedPrice;
roundedPrice = Math.round(n*100)/100;
console.log(roundedPrice); // It returns 17.95 instead of 17.96
It is happening for some specific values like 16.955, 17.955, 18.955, 19.955. Except these values like 1.955, 12.955, 20.955, 27.955 ... This round function return correct values.
Edited : It is happening with 17.955 only. This returns correct result with 17.9555 ( 3 times 5).
Thanks in advance.
You can use either Math.ceil() to get the expected result.

String to number is wrong [duplicate]

This question already has answers here:
Large numbers erroneously rounded in JavaScript
(6 answers)
Javascript parseInt() with leading zeros
(7 answers)
Closed 7 years ago.
I'm trying to convert a string to number in Javascript, but here is my problem :
var string = "068999999501111443";
parseInt(string); // 68999999501111440
Number(string); // 68999999501111440
Why does this happen and how to fix it ?
Thanks
This is because the number is too large to be stored accurately. It is being stored as a floating point number, which can only store a certain amount of precision. Beyond it's maximum precision, you'll get what look like weird rounding errors.
You'll get similar effects for decimals with a large number of decimal places. This is more well known, as it tends to occur more often, but it's exactly the same effect that is happening here.

JavaScript - Keep trailing zeroes [duplicate]

This question already has answers here:
How to add a trailing zero to a price?
(3 answers)
Closed 8 years ago.
I want to parse a string and I used parseFloat(), but it removes all the trailing zeroes. How to prevent this - I need to parse the string exactly - if I have 2.5000, I need exactly the same result as a floating-point number - 2.5000.
You can do
parseFloat(2.5).toFixed(4);
If you need exactly the same floating point you may have to figure out the amount
function parseFloatToFixed(string) {
return parseFloat(string).toFixed(string.split('.')[1].length);
}
console.log(parseFloatToFixed('2.54355'));
But i don't really understand why you even need to use parseFloat then? Numbers in javascript do not retain the floating-point count. so you would have to keep them as strings, and calculate against them as floats.
Also don't forget toFixed may have weird rounding issues in different browsers, for example
console.log((0.1).toFixed(20));

Categories

Resources