简体   繁体   中英

how to round the decimal number to the nearest tenth in javascript

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

}



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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM