Why toFixed() in Javascript acts like that? [duplicate] - javascript

This question already has answers here:
toFixed function not working properly ( please give a reason not an alternative)
(3 answers)
Why are floating point numbers inaccurate?
(5 answers)
Closed 5 years ago.
In this example/behaviour is something very strange.
Why does the function toFixed for the first two examples work and for this last one not?
//example 1
var num = 554.956;
var n = num.toFixed(2)
console.log(n);
var num2 = 554.955;
var n2 = num2.toFixed(2)
console.log(n2);
//output 554.96 and 554.96
//example 2
var num5 = 5.956;
var n5 = num5.toFixed(2)
console.log(n5);
var num6 = 5.955;
var n6 = num6.toFixed(2)
console.log(n6);
//output 5.96 and 5.96
//example 3
var num3 = 55.956;
var n3 = num3.toFixed(2)
console.log(n3);
var num4 = 55.955;
var n4 = num4.toFixed(2)
console.log(n4);
//output 55.96 and 55.95
Related to: php round vs javascript toFixed
EDIT: about the duplicates especially this one: toFixed function not working properly ( please give a reason not an alternative)
The answer is very good and helps me to understand the difference between x.955 and x.956, but doesn't answer why this happens only to the 55.955 in my example and not to the 5.955 or 554.955.

Related

Javascript calculates large integers incorrectly [duplicate]

This question already has answers here:
Extremely large numbers in javascript
(5 answers)
Closed 2 years ago.
I am making a program about the formula 10^(a-1)-1 mod a = 0. However, when using Javascript, it doesn't work with numbers for a above 23. Example:
var b = 29
var a = Math.pow(10, b-1)
console.log(a);
console.log(a/b);
console.log(a % b)
This is the output:
1e+28
3.448275862068965e+26
14
The output for the modulo function should be 1. Is there any way to make it solve the calculations correctly?
You can use BigInt.
var b = 29n;
var a = 10n ** (b - 1n);
console.log(a.toString());
console.log((a/b).toString());
console.log((a % b).toString());

How to round numbers (only in result at decimal part) in javascript? [duplicate]

This question already has answers here:
How to round to at most 2 decimal places, if necessary
(91 answers)
Closed 2 years ago.
So I'm having a problem of getting decimals. I can use them but when I do a calculation, that does not work. There's a lot of things to fix but is there any property that can help me?
let rank = prompt('Rank?', ''); // 2000
let x = prompt ('Value of x?', ''); // 90
let y = prompt ('Value of y?', ''); //1.6
var a = parseInt(rank); // 2000
var b = parseInt(x); // 90
var c = parseInt(y); // 1.6
var d = ((2000 - (500 * (3 - c)))/1000); // I get '1' instead of '1.3'
var e = d*b; // I get '90' instead of '117' (1.3*90)
var f = e*(y*y); // 334.0000004 (2*117) instead of 299.52 (2.56*117)
var g = b*(1-c); // 0 (90*(1-1)) instead of -54 (90*(1-1.6))
var h = a/90; // 22.2 is correct (2000/90)
var i = 2-c; // 0 because it rounds 1.6 to 2 (should be 0.4)
var j = h*i; // 0 and it should be 22.2*0.4
I just want to get rounded numbers in decimal part like 22.22222 becomes 22.2.
you want parseFloat, not parseInt
toFixed(1); // if you want rounded to 1 decimal place

Save and parseInt user input in one line of code? (Javascript) [duplicate]

This question already has answers here:
Why is parseInt() not working? [duplicate]
(4 answers)
Closed 3 years ago.
Here's my code:
function add() {
var num1 = prompt("Enter 1st number")
var num2 = prompt("Enter 2nd number")
parseInt(num1);
parseInt(num2);
var result = num1 + num2;
alert(result);
}
add();
I'm trying to build a simple addition calculator. parseInt wasn't working and an answer here said to declare the variable like var num1 = parseInt(num1);. However since I'm only getting "num1" through user input (var num1 = prompt..."), not sure how to store it as integer, or parseInt, in the same line. Any advice? Thanks.
All you have here are standalone, unused expressions:
parseInt(num1);
parseInt(num2);
Those evaluate to numbers, but you don't use them, so they're useless. Either assign them to variables, eg
const actualNum1 = parseInt(num1);
const actualNum2 = parseInt(num2);
and then use those variables, or just wrap the prompt in parseInt:
var num1 = parseInt(prompt("Enter 1st number"))
var num2 = parseInt(prompt("Enter 2nd number"))
Unless you're intending to only accept integers, consider using Number instead:
var num1 = Number(prompt("Enter 1st number"))
var num2 = Number(prompt("Enter 2nd number"))

How do I fix it. 50(10/100 + 1). Expect result = 55. But i got 55.000000001 in Javascript [duplicate]

This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 6 years ago.
I have
var x = 100;
var y = 10;
var b = 10 /100 + 1;
var z = b*50
Expect z = 55. But I got z = 55.0000000001. I don't know why.
How do I fix it in Javascript.
Thanks
Use:
z = parseInt(z);
It will treat z as int.
Use toFixed for digits after the decimal point. Default is 0.
var x = 100;
var y = 10;
var b = 10 /100 + 1;
var z = b*50;
alert(z.toFixed(0));
alert(z.toFixed()); //both are same
For more reference : http://www.w3schools.com/jsref/jsref_tofixed.asp

JS round to 2 decimal places [duplicate]

This question already has answers here:
How to round to at most 2 decimal places, if necessary
(91 answers)
Closed 7 years ago.
I am trying to limit the returned number to be only 2 decimal places but this code isn't working for me;
function myFunction() {
var x = document.getElementById("mySelect").value;
document.getElementById("demo").innerHTML = "Result is: " + x * 1.09; value = valToRound.toFixed(2);
}
What am I doing wrong?
Typing in the JS Browser console
x = 2.71828
x.toFixed(2)
"2.72"
it is clear that .toFixed(2) works
What you did wrong was rounding after printing the answer, and not using the correct variables.
document.getElementById("demo").innerHTML = "Result is: " + x * 1.09; value = valToRound.toFixed(2);
It is also a good idea to get in the habit of converting strings to numbers with parseFloat(). In JS, '2'*'2' is '4' but '2'+'2' is '22', unless you first convert to number.
If you do it this way it will work:
function myFunction() {
var x = parseFloat(document.getElementById("mySelect").value);
var valToRound = x * 1.09;
var value = valToRound.toFixed(2);
document.getElementByID("demo").innerHTML = "Result is: " + value;
}

Categories