Why does subtracting decimals not work in javascript [duplicate] - javascript

This question already has answers here:
difference between parseInt() and parseFloat() [duplicate]
(2 answers)
Closed 10 months ago.
If I do the code below and enter a decimal for one or both of the numbers, lets says I use 0.5 and 0.3, I should get 0.2 but I get 0 only. This makes no sense at all to me, it is probably a problem with using prompt but I need to use prompt or a method that is similar to prompt(I'm using sweetalert2 input for the alert). I am okay with using any js libraries.
const x = parseInt(prompt('1'))
const y = parseInt(prompt('2'))
alert(x-y)
I know it is a weird problem, but I don't know how to fix it.

You need to use parseFloat, not parseInt. parseInt is whole numbers only, while parseFloat allows decimal places.
parseFloat('0.9') === 0.9
parseInt('0.9') === 0

Related

How to convert "23.99999999999999" (repeating decimal with a repetend of 9) to "24" (correct integer) in JavaScript? [duplicate]

This question already has answers here:
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 24 days ago.
When I execute the JavaScript code 8/(3-8/3), the result is displayed as 23.99999999999999, but I want to fix it to 24.
In order not to round numbers when they are not repeating decimals or when their repetend is not 9 (e.g. do not round 21.835 to 22; do not round 2.979797979797 to 3), how should I solve this problem?
There doesn't seem to be a clear way to resolve this issue without using some sort of rounding function, but there are alternative ways to write this specific equation, according to MathsIsFun
E.g.
8/(1/3)

What's the deal with big number faulty arithematic in JS? [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)
Large numbers erroneously rounded in JavaScript
(6 answers)
javascript large integer round because precision? (why?)
(2 answers)
Closed 1 year ago.
So, I found something I couldn't understand and can't find any internet resource that explains it.
Please see the code below:
var num = 35422484817926290
// subtract 5 from num
console.log(num-5)
Output (Wrong) : 35422484817926284
I checked it in Node, Opera, and Chrome, all of them give the wrong answers.
I do understand the fact that arithmetic with unsafe Integers in JS is faulty, for example:
console.log(100000000000000005-1)
Output (Wrong) : 100000000000000000
So what's the deal with big number arithmetic in JS?
When I run this code:
var num = 35422484817926290
// subtract 5 from num
console.log(num-5)
in Visual Studio Code, i get the following warning:
"Numeric literals with absolute values equal to 2^53 or greater are too large to be represented accurately as integers."
So the correct way to make this calculation would be like this:
var num = 35422484817926290n
// subtract 5 from num
console.log(num-5n)
JavaScript is not faulty, this is how the Floating point arithmetic works. Looks duplicate to this post. For better calculation involving floating-point numbers you should use BigNumber API.

How to round numbers with N digits after the zero? [duplicate]

This question already has answers here:
Formatting a number with exactly two decimals in JavaScript
(32 answers)
Closed 6 years ago.
lets say I have a number in the following form: 0.00N1N2N3...(for example 0.007).
I want to round the number 0.00N1N2N3...Nn, into the follwoing number:
0.0M1M2M3..Mn.
For example:0.007 need to be round to 0.01.
Now the number can be also in the following form 0.N1...Nn or N1.N2...Nn so the solution need to be generic for all cases.
I have write the following function(Not sure if this is the right answer):
function roundup(number, precision) {
return Math.ceil(number * precision)
}
If the variable is float you can use toFixed() like
var formatted = parseFloat("345.65894").toFixed(2);
On most browsers you can use the toFixed() function.
number.toFixed(precision)
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

Convert Javascript number to two decimal places [duplicate]

This question already has answers here:
What is the equivalent of NumberFormat in JavaScript or JQuery?
(5 answers)
Closed 7 years ago.
I know there have been many threads on this topic but none seem to answer my question directly. I want to convert a number to two decimal places no matter the length. So 0.5 should turn into 0.50, and 5.3145 should go to 5.31. Most importantly, I also need to keep the number as a number datatype. I know toFixed(2) will create two decimal places, but this also turns the number into a string. If I wrap the toFixed(2) with a parseInt function (e.g. parseInt(amount.toFixed(2))) then 0.5 seems to be converted to "0.50", and then back to 0.5 with out the trailing zero. Adding a second layer of parentheses doesn't solve the problem either. Any ideas?? Thank you in advance!
What you want to do is impossible to do. Numbers do not have trailing zeros. There are no significant digits. So when you need the trailing zero, you need to convert it to a string with toFixed().
You don't need trailing zeros for mathematical computations - so if you are just looking to display it - keep it a string and use the toFixed(2) then when you need to do computations on it - convert with parseFloat

How to get 12.6 with a=10.3 and b=2.3? [duplicate]

This question already has answers here:
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 8 years ago.
Tried :
var a=10.3;
var b=2.3;
alert(a+b);
but I get 12.600000000000001. I know JavaScript is loosely typed, but I hope I can do a sum :)
you can use toFixed() method also
var a=10.3;
var b=2.3;
alert((a+b).toFixed(1));​
Works in chrome
Multiply to the precision you want then round and divide by whatever you multiplied by:
var a=10.3;
var b=2.3;
alert(Math.round((a+b) * 10) / 10);
http://jsfiddle.net/DYKJB/3/
It's not about the typing but about the precision of floating point types. You need to round for presentation.
Floating point types are not a good choice if you need exact values. If you want to express currency values express them as cents or use an appropriate library for this.

Categories