Decimals are not percise [duplicate] - javascript

This question already has answers here:
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 4 years ago.
If I do 0.3 - 0.2 it gives 0.9999999998 not 0.1 is there a way to make it give a percise decimal? Can't trust a calculator if it's not percise.

You could try this solution: https://30secondsofinterviews.org/#what-does-0-1-0-2-0-3-evaluate-to-
A solution to this problem would be to use a function that determines if two numbers are approximately equal by defining an error margin (epsilon) value that the difference between two values should be less than.
const approxEqual = (n1, n2, epsilon = 0.0001) => Math.abs(n1 - n2) < epsilon
approxEqual(0.1 + 0.2, 0.3) // true

Here is a very quick answer that sums up all of the flags as best I can:
Use, for standard math:
(.3-.2).toFixed(10); //10 can be changed to whatever you like for your string
If you need to do more math with it than do:
Number((.3-.2).toFixed(10));
This last one will give you exactly what you expected in a number type.

Related

How do I round a number in js with negative precision like in Excel round function [duplicate]

This question already has answers here:
How do I round a number in JavaScript?
(8 answers)
Closed 5 years ago.
I am trying to round the value from 8904000 to 8900000 is there any possible way to change this using Math.round.
example in MS Excel
=round(8904000,-5);
ANS: 8900000
I tried the code below but Math.round does not take a second argument
Math.round(8904000,-5);
ANS: 8904000
In short I want to round of the number to the nearest 100,000 number
So that 5300*56*30= 8904000 becomes 8900000
Try this,
function roundDown(number, decimals) {
decimals = decimals || 0;
return ( Math.floor( number * Math.pow(10, decimals) ) / Math.pow(10, decimals) );
}
roundDown(8904000, -5)
The common solutions for rounding to a decimal place is:
Using Number.prototype.toFixed()
Multiply the float by some power of 10 in order to leverage Math.round().
Both of these work, except sometimes a decimal of 5 is rounded down instead of up.The rounding problem can be avoided by using numbers represented in exponential notation
Try this
function round(value, decimals) {
return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}
round(8904000, 5)
Math.round(value/1000000)*1000000
Divide the value by the rounding unit, hence 1000000, Math.round() the number and multiply with the specified unit.

Javascript parsefloat bad subtraction [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
I am going to subtract two floats but I get 999998.7799999999 why ? It real result is 999998.78
Money1="2,000,001.44 $";
Money2="1,000,002.66 $"
Money1= Number(Money1.replace(/[^0-9\.]+/g,""));
Money2= Number(Money2.replace(/[^0-9\.]+/g,""));
console.log(parseFloat(Money1)-parseFloat(Money2));
You can use toFixed(2) because you are working only with two digits:
toFixed() returns a string representation of numObj that does not use exponential notation and has exactly digits digits after the decimal place. The number is rounded if necessary, and the fractional part is padded with zeros if necessary so that it has the specified length. If numObj is greater than 1e+21, this method simply calls Number.prototype.toString() and returns a string in exponential notation.
The snippet:
Money1="2,000,001.44 $";
Money2="1,000,002.66 $"
Money1= Number(Money1.replace(/[^0-9\.]+/g,""));
Money2= Number(Money2.replace(/[^0-9\.]+/g,""));
result = (Money1 - Money2).toFixed(2);
console.log(result);
Your Problem is related to floating point precision.
You can find a explanation here.
The result you want is just a rounded version of the what Javascript is producing (read more about floating point operations in Javascript). You can round the result yourself to get the formatting you desire:
Money1="2,000,001.44 $";
Money2="1,000,002.66 $"
Money1= Number(Money1.replace(/[^0-9\.]+/g,""));
Money2= Number(Money2.replace(/[^0-9\.]+/g,""));
Rounded = Math.round((parseFloat(Money1)-parseFloat(Money2)) * 100) / 100;
console.log(Rounded);
The result is due to floating point precision, the Python docs have a great explanation. https://docs.python.org/2/tutorial/floatingpoint.html

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 avoid 0.2 + 0.4 = 0.6000000000000001 other than Math.round() [duplicate]

This question already has answers here:
How to deal with floating point number precision in JavaScript?
(47 answers)
Dealing with float precision in Javascript [duplicate]
(5 answers)
Closed 9 years ago.
Is there any better way other than
Math.round(0.2+0.4) = 0.6
Actually, I have a series of index 0, 0.02, 0.04, 0.06, 0.08, 0.10 ----
I have to convert it to Array Index 0,1,2,3,4,5-- by dividing by 0.02
JavaScript provides
0.28/0.02 = 14.000000000000002
I solve this problem by Math.round(0.28/0.02).
I am curious is there any other or better way to solve this problem
JS Number class is 64-bit floating-point, so it's bound to lose precision in corner cases like this one (also for integers > 2^53).
It's OK to use Math.round and their friends in your case. For a full discussion of the problem and possible workarounds, see the article provided by #Bergi.
I wonder isn't that easier in your particular case to precache the hash of indexes, like this:
var fpIndexes = ['0', '0.02', '0.04', '0.06'];
var fpIndexesHash = {};
for (var i = 0, l = fpIndexes.length; i < l; i++) {
fpIndexesHash[fpIndexes[i]] = i;
}
Then you'll be able to get the integer index out of floating-point value without doing Math.round:
someValues[fpIndexesHash['0.02']]; // the same as someValues[1];
I've used strings here, but I actually think plain floating-point literals can be used here too: my understanding is that a specific floating-point literal will never be represented by different IEEE-754 values at the same machine during the lifetime of a script.
So even if 0.06 literal is actually represented as 0.060000001 number in memory, it'll be uniform - and will be converted to the same string value both in fpIndexesHash assigning round and in the interpreting one.

Expression solver for Javascript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Elegant workaround for JavaScript floating point number problem
I need a workaround for the fact that Javascript can't do floating math reliably due to the IEEE 754 standard from 1985 it uses. Basically, what I need is a way to evaluate an expression like 5+3*(2+8/6). I'm thinking of doing it with strings and rolling my own functions for basic operations (+-*/%), but I was wondering first if you know of any library that does this already.
It depends on how big the numbers are. For small numbers (e.g. fewer than 14 significant digits), rounding to acceptable precision may do the job. e.g. given your example:
var n = 0.5 / 5 + 1 / 5; // 0.30000000000000004
var p = Math.pow(10, 14); // where 14 is calculated based on the first
// significant digit and its relationship to
// the decimal place
n = Math.round(n * p) / p; // 0.3
Calculating the power of 10 to use shouldn't be that hard. If you have large numbers, then life is more difficult (see existing libraries).

Categories

Resources