how to round a number to 2 decimal place? javascript [duplicate] - javascript

This question already has answers here:
Formatting a number with exactly two decimals in JavaScript
(32 answers)
Closed 7 years ago.
Im trying to round a number to 2 decimal place. I have tried the following but not having any luck? Can somebody please help me and tell me where im going wrong??
var winPercentage = totalWins/(totalWins+totalLost)*100;
winPercentage.toFixed(2);
document.getElementById('win-percentage').innerHTML = winPercentage + " %";
i search and tried this but to be honest i have no idea what it is?
var winPercentage = totalWins/(totalWins+totalLost)*100;
expr {double(round(100*winPercentage))/100}
document.getElementById('win-percentage').innerHTML = winPercentage + " %";

Try to use the following syntax instead and alter it to your needs
var num = 5.1;
num.toFixed(2); //will become 5.10

You had the right idea with toFixed(2). The problem is that it returns the formatted number, it does not alter the variable it was called on. In other words, you just need to assign it back the the variable you were using:
winPercentage = winPercentage.toFixed(2);

Related

Javascript floating issue with addition [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Float sum with javascript [duplicate]
(2 answers)
Closed 3 years ago.
I have following value which give wrong total.
let a = 86.2500;
let b = 32.3550;
alert(a+b); //return 118.60499999999999 , expected 118.605
alert((a+b).toFixed(2)) //return 118.60 , expected 118.61
When I calculate above value with my calculator it give my expected result but javascript give me unexpected result. Why and what is solution to get expected result?
https://jsfiddle.net/vnu9fyb8/1/
Try this to get the result:
let a = 86.2500;
let b = 32.3550;
alert((a+b).toFixed(3));
var digit = parseFloat((a+b).toFixed(3)).toFixed(2);
alert(digit);
I hope it will work for you.
toFixed(2) cannot make it become 118.61 because of the full result is 118.60499999999999 so "6049" in front of 0 is number 4. 4 can't be floored up to be 5, but if in front of 0 is number 5 it sure will be floored up to 118.61.
So this can't become 118.61.
You can refer to this link

JS how to add instead of concatenate numbers [duplicate]

This question already has answers here:
Javascript variables not adding two variables correctly, only concatenating
(3 answers)
Closed 4 years ago.
This question has an answer:
see - Javascript variables not adding two variables correctly, only concatenating
I am trying to add 5 units to a number but the number is being concatenated instead.
this.graphicState[i].shapes[j][k].x += 5
Each time this is run in a loop the outputs are
105.00
105.005
105.0055
105.00555
...
The output I am looking for is,
105.00
110.00
115.00
120.00
...
I tried,
this.graphicState[i].shapes[j][k].x += parseFloat(5)
I also tried this, but get the same results,
this.graphicState[i].shapes[j][k].x = this.graphicState[i].shapes[j][k].x + 5
Thanks,
You need to convert any/all string values to numbers in an expression that has strings as operands with + as the operator. The 5 isn't the issue, this is:
this.graphicState[i].shapes[j][k].x
So, that's what needs to be converted. You can do that easily by prepending a + to it:
+this.graphicState[i].shapes[j][k].x;
Do the conversion on the string first and then use the converted value in your mathematical expression. Here's a simplified example:
var result = "5"
result = +result + 10;
console.log(result);
Try this method
this.graphicState[i].shapes[j][k].x = (parseFloat(this.graphicState[i].shapes[j][k].x) + 5).toFixed(2);

Multiply a string in JS [duplicate]

This question already has answers here:
Repeat a string in JavaScript a number of times
(24 answers)
Closed 4 years ago.
i want to display a string as many times I have a generated variable. Therefore I'd like to do something like that, which doesn't work
var shower_total = 7; // this gets generated, but for simplification...
var uhrzeit = "<p class='clock_item'>Foo</p>";
document.getElementById("uhrzeit_block").innerHTML =5*uhrzeit;
That's why I tried looping it but that doesn't work neither
document.getElementById("uhrzeit_block").innerHTML =
for(b=0, b <= shower_total; b++){
uhrzeit
};
What do I do wrong or what would be a possible - beginner-compatible - solution. Thanks!
You could use String#repeat instead of a multiplication of a string. This does not work for value who can not converted to a number.
var uhrzeit = "<p class='clock_item'>Foo</p>";
console.log(uhrzeit.repeat(5));

Addition not functioning correctly [duplicate]

This question already has answers here:
Addition operation issues?
(5 answers)
Addition is not working in JavaScript
(7 answers)
Closed 7 years ago.
Somebody please help me. I'm not sure what I'm doing wrong here:
I'm trying to add up some numbers.
Instead the adding the 2 numbers it is just placing the 2 numbers next to each other; as in:
3 + 3 = 33 (instead of 6) !!!
If I replace the + with a * then it returns the correct result (9)
What is happening?
<p id="A">3</p>
<p id="B">3</p>
<p id="score"></p>
<script>
var AA = document.getElementById("A").innerHTML;
var BB = document.getElementById("B").innerHTML;
var result = AA + BB;
document.getElementById("score").innerHTML = result;
</script>
That's because they're strings so you're getting string concatenation. Convert them to numbers before performing addition.
AA = parseInt(AA, 10);
BB = parseInt(BB, 10);
What you are getting back from the html are strings, not numbers. You need to parse them as numbers first:
var AA = parseInt(document.getElementById("A").innerHTML, 10);
var BB = parseInt(document.getElementById("B").innerHTML, 10);
parseInt will convert the string to a number, and make your addition work correctly. The second parameter in the parseInt call is the radix, which will help prevent confusing mishaps when it comes to actually parsing the numbers

Variables and math operators [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Strange javascript addition problem
I know there's obviously a solution for this, and I've done it before, but I can't remember it and now I can't find it.
​<div>1</div>​​​​​​​​
$(function() {
var number = $('div').text();
var math = number + 2;
$('body').text(math);
});​
http://jsfiddle.net/G5zdx/
number is not being treated as an integer so math's value is "12" instead of "3". How can I correct this?
There are many ways, but
var math = (1 * number) + 2;
is a simple one. Whether you should be detecting possible ill-formed non-numbers depends on the nature of the rest of your code.
The parseInt() function is useful, but it probably should be called with 10 as its second argument to avoid interpreting numbers that begin with zero as octal constants instead of decimal. Also, parseInt() will not treat a string like "23skidoo" as an error, which may or may not be OK in your application.
​<div>1</div>​​​​​​​​
$(function(){
var number = parseInt($('div').text(), 10);
var math = number+2;
$('body').text(math);
});​
You can parse a string as an int using parseInt(string[, radix]).
Your code would look as follows
$(function(){
var number = $('div').text();
var math = parseInt(number, 10)+2;
$('body').text(math);
});
Source: http://www.javascripter.net/faq/convert2.htm

Categories

Resources