0
**<script>
function roundNum() {
var f = document.getElementById("f").value;
var g = document.getElementById("g").value
var g = g-u
document.getElementById("ng").innerHTML = g
</script>**

I am trying to receive a three-digit answer (example: 95.7, instead of 95.6923531)

csme
  • 33
  • 6
  • 1
    You can use `.toFixed()` to convert the number to a string with a maximum number of fraction digits. You cannot restrict actual numbers to a specific number of decimal places because numbers are *binary* floating point. – Pointy Nov 19 '19 at 14:57
  • 1
    when you have a problem like this just use google. the chances that you are the first person who wants to format a number is equal to 0 ;) https://stackoverflow.com/questions/4098685/rounding-numbers-to-2-digits-after-comma/32761885 – hansTheFranz Nov 19 '19 at 14:58

2 Answers2

0

You can use the javascript toFixed() method.

Example:

var x = 9.656;
x.toFixed(1);           // returns 9.7
x.toFixed(2);           // returns 9.66
x.toFixed(4);           // returns 9.6560
x.toFixed(6);           // returns 9.656000
ziga1337
  • 144
  • 1
  • 10
0

Try this..

Syntax: number.toFixed(x)

var num = 95.6923531;
console.log(num.toFixed(1));
console.log(num.toFixed(2));
console.log(num.toFixed(3));
hbamithkumara
  • 2,344
  • 1
  • 17
  • 17