PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 10, 2022

[FIXED] how to round the decimal number to the nearest tenth in javascript

 August 10, 2022     decimal, javascript     No comments   

Issue

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

}




Solution

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


Answered By - chad_
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing