How do you round to 1 decimal place in Javascript?

前端 未结 21 1440
难免孤独
难免孤独 2020-11-22 08:49

Can you round a number in javascript to 1 character after the decimal point (properly rounded)?

I tried the *10, round, /10 but it leaves two decimals at the end of

相关标签:
21条回答
  • 2020-11-22 08:59

    If you use Math.round(5.01) you will get 5 instead of 5.0.

    If you use toFixed you run into rounding issues.

    If you want the best of both worlds combine the two:

    (Math.round(5.01 * 10) / 10).toFixed(1)
    

    You might want to create a function for this:

    function roundedToFixed(_float, _digits){
      var rounded = Math.pow(10, _digits);
      return (Math.round(_float * rounded) / rounded).toFixed(_digits);
    }
    
    0 讨论(0)
  • 2020-11-22 09:01

    Try with this:

    var original=28.453
    
    // 1.- round "original" to two decimals
    var result = Math.round (original * 100) / 100  //returns 28.45
    
    // 2.- round "original" to 1 decimal
    var result = Math.round (original * 10) / 10  //returns 28.5
    
    // 3.- round 8.111111 to 3 decimals
    var result = Math.round (8.111111 * 1000) / 1000  //returns 8.111
    

    less complicated and easier to implement...

    with this, you can create a function to do:

    function RoundAndFix (n, d) {
        var m = Math.pow (10, d);
        return Math.round (n * m) / m;
    }
    

    function RoundAndFix (n, d) {
        var m = Math.pow (10, d);
        return Math.round (n * m) / m;
    }
    console.log (RoundAndFix(8.111111, 3));

    EDIT: see this How to round using ROUND HALF UP. Rounding mode that most of us were taught in grade school

    0 讨论(0)
  • 2020-11-22 09:01

    Why not just

    let myNumber = 213.27321;
    +myNumber.toFixed(1); // => 213.3
    
    1. toFixed: returns a string representing the given number using fixed-point notation.
    2. Unary plus (+): The unary plus operator precedes its operand and evaluates to its operand but attempts to convert it into a number, if it isn't already.
    0 讨论(0)
  • 2020-11-22 09:01

    If your method does not work, plz post your code.

    However,you could accomplish the rounding off task as:

    var value = Math.round(234.567*100)/100
    

    Will give you 234.56

    Similarly

     var value = Math.round(234.567*10)/10
    

    Will give 234.5

    In this way you can use a variable in the place of the constant as used above.

    0 讨论(0)
  • 2020-11-22 09:04
    var num = 34.7654;
    
    num = Math.round(num * 10) / 10;
    
    console.log(num); // Logs: 34.8
    
    0 讨论(0)
  • 2020-11-22 09:04

    I found a way to avoid the precision problems:

    function badRound (num, precision) {
        const x = 10 ** precision;
        return Math.round(num * x) / x
    }
    // badRound(1.005, 2) --> 1
    
    function round (num, precision) {
        const x = 10 ** (precision + 1);
        const y = 10 ** precision;
        return Math.round(Math.round(num * x) / 10) / y
    }
    // round(1.005, 2) --> 1.01
    
    0 讨论(0)
提交回复
热议问题