Reputation: 95
I'm a beginner to Javascript I,m trying to creat calories calculator but the result is always decimal number like this ( 2758.59375) I want to know how to round it to nearest tenth like ( 2758.6) and thank you That is my code if it was necessary
var
weight = prompt ("enter your weight in kg ");
height = prompt ("enter your height in cm");
age = prompt ("enter your age");
train = prompt ("how many days you train per week");
calories=(weight*10 )+(height*6.25)-(age *5) +5;
low = 1.25;
med = 1.375;
high = 1.505;
veryhigh = 1.725;
if(train >=0)
{
document.getElementById("test").innerHTML="calories you need is: " +calories * low + " calcalorie" ;
}
Upvotes: 0
Views: 411
Reputation: 3819
Do you actually want a number like 2758.59375 to round down to 2758.5? I would expect that number to be rounded to 2758.6, personally. Depending if you want it to round down, round up, or round to nearest, the code is slightly different.
The simplest way is to say:
> var num = 2758.59375
> num.toFixed(1)
2758.6
Upvotes: 1