JS round to 2 decimal places [duplicate] - javascript

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;
}

Related

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

split integer add decimal point and return value 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.
My integer value is like below and
var amount= 5501;
var amount= 5500;
var amount= 5560;
var amount= 5563;
var amount= 5510;
i want split this integer like below .Have to add decimal point on middle. if last digit 0 not consider.
amount and should come "55.0.1"
amount and should come "55.0"
amount and should come "55.6"
amount and should come "55.6.3"
amount and should come "55.1"
i tried this ,
var variable1 = 5500;
function versionss(variable1){
var digits = (""+variable1).split("");
if(Number(digits[3] > 0)){
return Number(variable1/100) ; //
} else {
return digits[0]+digits[1]+'.'+digits[2];
}
}
Using Number.isInteger()
function versionss(amount) {
var divisor = Number(amount) > 999 ? 100 : 10;
var value = amount / divisor;
return Number.isInteger(value) ? value.toFixed(1) : value;
}
console.log(versionss(5501));
console.log(versionss(5500));
console.log(versionss(5560));
console.log(versionss(5563));
console.log(versionss(5510));

Why is one form input constantly being treated as a string? [duplicate]

This question already has answers here:
HTML input type="number" still returning a string when accessed from javascript
(9 answers)
Closed 3 years ago.
This is a pretty basic SIP calculator that should not take more than 5 lines. But the variable amount is constantly being treated as a string. I have to keep using parseFloat() and declare three additional variables to store the final values before returning them for the code to work. Is there any workaround?
function sipCalculator(amount, r, n) {
r = r / 12;
amount = parseFloat(amount);
var temp = 0;
for (var i = 0; i < n; i++) {
temp += amount;
temp += (temp * (r / 100));
}
var x = amount * n;
var y = parseFloat(temp.toFixed(2));
var z = parseFloat((y - x).toFixed(2));
return [x, y, z];
};
The call to .toFixed(2) is converting the numbers to a string. If you instead want them to be numbers, while limiting them up to two decimal places, then parseFloat(num.toFixed(2)) is the recommended approach (which you are already doing).
Here's MDN's documentation on Number.prototype.toFixed: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed
If you just don't like the verbosity of parseFloat, you can use + instead to convert the string to a number. Of course that's not the best idea as not everyone would know what it does.
var x = 1.44444;
console.log(+x.toFixed(2));

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

Format float with two decimals without using .toFixed() [duplicate]

This question already has an answer here:
Javascript Adding Two Decimal Places
(1 answer)
Closed 9 years ago.
I have a float,
var a = 324620.8
and I want it to look like this
a = 324620.80
This is my code so far,
var a_float = a;
var a_int = parseInt(a);
d = a_float - a_int;
if(d <= 0){
a = a_int+'.00';
}else{
if(d < 0 && d > 0.1){
a = a_int + d + '0';
}else{
a = a_float;
}
}
This would works for only one decimal digit.
I want it to work when I have 2 decimal digits.
.toFixed would not work in some browsers.
Answering the question in the title
How to find how many decimal digits in a float?
Compare position of '.' to length of float as a String.
var x = 1.2345,
x_str = x.toString(),
decimal_digits = x_str.length - x_str.lastIndexOf('.') - 1;
decimal_digits === x_str.length && (decimal_digits = 0); // case no decimal
decimal_digits; // 4
JSFIDDLE
Use toFixed("2");
var f = 1.3454545;
f.toFixed(2);
var decimal = 4.0;
var a = (decimal).toFixed(2);
console.log(a); // outputs 4.00

Categories