not rounding tofixed [duplicate] - javascript

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

Related

String to int rounding number on nodeJS app [duplicate]

This question already has answers here:
What is JavaScript's highest integer value that a number can go to without losing precision?
(21 answers)
What is the standard solution in JavaScript for handling big numbers (BigNum)?
(1 answer)
Closed 1 year ago.
I have a string as numbers. And I want to transform string to int.
So my code like that:
const bigNumber = '6972173290701864962'
console.log(bigNumber)
//6972173290701864962 =====> last digits : *****1864962
console.log(Number(bigNumber))
//6972173290701865000 =====> last digits : *****1865000
Why Im getting rounding number? How can I solve this problem?
The number is greater than Number.MAX_SAFE_INTEGER.
Instead, cast a to a BigInt:
const bigNumber = '6972173290701864962'
console.log('Number: '+Number(bigNumber))
console.log('BigInt: '+BigInt(bigNumber))
To remove the trailing n, simply call toString():
const bigNumber = '6972173290701864962'
console.log(BigInt(bigNumber).toString());

Finding number of digits in javascript [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
I'm trying to get the number of digits of a number in javascript, but I'm running into some edge cases with the equations I've found online.
Here's what I'm using, from this site.
getNumDigits(val){
return val === 0 ? 1 : Math.floor(Math.log(Math.abs(val)) / Math.LN10 + 1);
}
But the problem with this is, if you put in a number like 1000, you somehow get a javascript rounding error where the value comes out like 3.999997
I've noticed that, as long as your number isn't between -1 and 1, you can just add 1 to the val in the Math.abs(val) and it will appropriately set the number of digits, but it just seems messy.
I've tried just converting the val into a string, and getting the length, but that doesn't work in the case of, say, a decimal, where you're using something like 0.2 - as it will say the length is 3.
What's a good equation / function to use?
Making the number a string and using a regex to only count the digits can work.
function getNumDigits(val) {
return (`${val}`.match(/\d/g) || []).length
}
console.log(getNumDigits(2.5245234))

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 math result lesser than 0 gets 0.0000000000 [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
Why does javascript returns so many zeros and not just 0.24
And how can i disable javascript to do this. Because when im using a calculator i never get the result 0.24000000000002
var sum = (0.0001 * 2400);
result 0.2400000000002
The reason for this is that your sum is a float which are known to not be very precise. This is a limitation of float values.
To fix this you need to round the decimals by either Math.round or .toFixed.
javascript always do that but you can make it show only 2 digits after the dot.
var sum = (0.0001 * 2400);
alert(sum.toFixed(2));

Javascript convert float to Exponential [duplicate]

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).

Categories

Resources