4

Possible Duplicate:
How do you round to 1 decimal place in Javascript?

The following code, displays the total distance covered, on a particular route, displayed on google maps. I managed to convert the number from kilometers to miles. Here is the code for the function:

function computeTotalDistance(result) {
        var total = 0;
        var myroute = result.routes[0];
        for (i = 0; i < myroute.legs.length; i++) {
          total += myroute.legs[i].distance.value;
        }
        total = total *0.621371/ 1000.
        document.getElementById('total').innerHTML = total + ' mi';

The total is displayed as 41.76483039399999 mi. How would round off the total to one decimal place?

Community
  • 1
  • 1
Robert Martens
  • 173
  • 4
  • 5
  • 14
  • https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Number/toFixed var n = 123.123; n.toFixed(1); // 123.1 – Brian Cray Jan 22 '13 at 14:41

4 Answers4

18

Use toFixed:

var total = 41.76483039399999;
total = total.toFixed(1) // 41.8

Here's the fiddle: http://jsfiddle.net/VsLp6/

Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • 2
    Be careful using `.toFixed()` as it might return different rounding results for different browsers. Read [**this post**](http://stackoverflow.com/questions/566564/javascript-functions-math-roundnum-vs-num-tofixed0-and-browser-inconsistenci) for details on the topic! – Wilt Mar 04 '14 at 13:10
  • I want to do it, but with a high value: example 749,640 now i expected: 750,000 how can i do it ? – AllisLove Jul 07 '21 at 17:59
  • i want to make 41.78 to 41.7 not 41.8 !! – Mojtaba Darzi Jan 09 '22 at 08:46
  • @MojtabaDarzi - For that you'll need `Math.floor()`, with multiplication and division for your precision: `Math.floor(41.78 * 10) / 10` will return `41.7`. – Joseph Silber Jan 10 '22 at 13:59
14
Math.round(total * 10) / 10

This results in a number. toFixed() gives a string, as detailed in other answers.

PinkElephantsOnParade
  • 6,452
  • 12
  • 53
  • 91
  • maybe because there's a built-in function for it (`.toFixed()`) which is more elegant than using Math.round(). Though there's nothing wrong with your solution and it shouldn't be down voted because technically it is not wrong. – AlexStack Jan 22 '13 at 14:42
  • @AlexStack The two different ways return different types - mine is numerical, toFixed() returns a String. – PinkElephantsOnParade Jan 22 '13 at 14:42
  • Good point. +1 for that from me. – AlexStack Jan 22 '13 at 14:43
1

You are looking for Number.prototype.toFixed; 41.76483039399999.toFixed(1) === "41.8";

function computeTotalDistance(result) {
    var total = 0, myroute = result.routes[0];
    for (i = 0; i < myroute.legs.length; i++) {
        total += myroute.legs[i].distance.value;
    }
    total = (total * 0.621371 / 1000).toFixed(1);
    document.getElementById('total').innerHTML = total + ' mi';
}

There are a very many other ways to achieve this, for example, without using any methods from Math or instances of Number

(~~(10 * total) + (~~(100 * total) % 10 >= 5))/10 + '' // "41.8"
// (      417   +     (6 >= 5)               )/10 = 41.8
Paul S.
  • 64,864
  • 9
  • 122
  • 138
0

There is a function to do what you want:

var total = 41.76483039399999; print(x.toFixed(2));

It will be printed 41.76

igarcia
  • 691
  • 1
  • 9
  • 26