decimal places limited to 15 in javascript [duplicate] - javascript

This question already has answers here:
What is the standard solution in JavaScript for handling big numbers (BigNum)?
(1 answer)
Is there a limit in Javascript to decimal places on big floats?
(1 answer)
Closed 5 years ago.
I'm trying to get a decimal number without any decimal places limit in javascript. I have two numbers in my code, 610 and 987. If I divide them in the default windows calculator, I get the number 1.6180339850173579389731408733784. This number has 31 decimal places. If I divide them using javascript, it automatically rounds this number and limits it to 15 decimal places. Since I need this number to calculate further, it has to be the full decimal number, otherwise the final result is wrong.
Currently I'm using this code:
var multiplicator = 987/610; // returns 1.618032786885246
var maxloop = input - 15;
for(var i = 0; i < maxloop; i++){
value *= multiplicator;
}
Since the variable multiplicator doesn't contain the whole decimal number, the value will be wrong for higher values, because of the rounding.
I already tried toFixed() function, but then I only get zeroes at the end of the decimal number instead of the correct decimal value and toFixed() allows only digits between 0 and 20.
var multiplicator = (987/610).toFixed(31); // wont work because toFixed() allows only digits between 0 and 20
Is there a way to get the full decimal number with all the 31 decimal places?

Related

String Convertion To Integer In JS Problem [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)
Closed 4 days ago.
I was expecting 6145390195186705543 but gave me 6145390195186705000.
This is my code:
var str = '6145390195186705543';
var num = parseInt(str);
console.log(num)
JavaScript uses 64-bit floating point numbers, which can represent numbers in the range -(2^53 - 1) to (2^53 - 1). In this case, the number 6145390195186705543 is out of this range and cannot be accurately represented in JavaScript. When trying to convert the string "6145390195186705543" to a number using the parseInt() function, JavaScript cannot represent the number exactly and rounds it to the nearest floating point number, resulting in 6145390195186705000.
To work with such large numbers, you can use special libraries for working with large arithmetic, such as BigInt or BigNumber.

Limit precision of the float number without rounding [duplicate]

This question already has answers here:
Truncate number to two decimal places without rounding
(43 answers)
Closed 2 years ago.
is there a way to have 2 numbers after comma without rounding the value. I want the exact value. Math.round() and toFixed() give the value rounded.
You can do the workaround with help of Math.ceil() and Math.floor() functions.
Another way, is treat is as an string and use .slice()
i.e:
number = number.slice(0, number.indexOf(".")+3); //this should give you 2 decimals
Number(number); //Convert it to "Number" again, so you can operate with it
Solution without type conversions
While solving the issue, you should bear in mind that bouncing back and forth between data types may cost you some of app performance wasted
Instead, I'd suggest to modify input number directly:
shift the dot n positions to the right by multiplying your number by 10 in power of n (10**n)
cut off what's left after dot, using bitwise OR (|) that implicitly turns the float into integer
divide the result by 10 in power of n to shift the dot n positions back to the left
Following is a quick live-demo:
const num = 3.14159265,
precision = 4,
limitPrecision = (n,p) => (0|n*10**p)/10**p
console.log(limitPrecision(num, precision))
.as-console-wrapper{min-height:100%;}

convert 4.5 to 4 but 4.6 to 5 in javascript [duplicate]

This question already has answers here:
JavaScript: Rounding Down in .5 Cases
(4 answers)
Closed 3 years ago.
I have to divide n number into 3 parts according to given rule but the divided number should not be a floating-point number if the given n is an odd number.
Example
`const rule = [50,30,20]` . // in percentagee
const n = 10;
the expected result should // 5,3,2
but if the given number is an odd number then the output should be as given below
n = 9
output 4,3,2
when getting the percentage according to rules it comes as follow
4.5,2.7,1.8
Now I want if the fractional part of the number is up to .5, the argument should be rounded to the next lower integer. If the fractional part of the number is greater than .5, the argument is rounded to the next higher integer.
This is NOT asking how to round floating point numbers, as the dupe target says. This is asking how to round DOWN .5 cases.
ANSWER: var result = -Math.round(-num);
You can do this by negating both the result and the argument to Math.round.
E.g. var result = -Math.round(-num);
The only thing this changes is the .5 case, as -4.5 rounds up to -4, then you simply change the sign again.

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

Problems with parseInt method [duplicate]

This question already has answers here:
parseInt rounds incorrectly
(2 answers)
Closed 7 years ago.
The parseInt function made my number loose precision: the last two digits changed from 18 to 20:
console.log(parseInt('76561198236425518', 10));
76561198236425520
Why did that happen and how to fix it?
Numbers are stored as floating point with a 53-bit manttissa. This limits the precision you can have to less than what you have in that number of yours, hence it has to round to the nearest floating point number it can represent.
The actual number of bits needed to represent a number N can be calculated as about log2N or, if you're working on a calculator that can't calculate logarithms to base two, logxN/logx2.
The value of log276561198236425518, roughly 56.1, shows that it requires about 56 bits, which is why it's not exact near the end.

Categories

Resources