Truncate number to two decimal places without rounding

前端 未结 30 2656
再見小時候
再見小時候 2020-11-22 09:08

Suppose I have a value of 15.7784514, I want to display it 15.77 with no rounding.

var num = parseFloat(15.7784514);
document.write(num.toFixed(1)+\"
相关标签:
30条回答
  • 2020-11-22 09:54

    Simple do this

    number = parseInt(number * 100)/100;
    
    0 讨论(0)
  • 2020-11-22 09:55

    Here is what is did it with string

    export function withoutRange(number) {
      const str = String(number);
      const dotPosition = str.indexOf('.');
      if (dotPosition > 0) {
        const length = str.substring().length;
        const end = length > 3 ? 3 : length;
        return str.substring(0, dotPosition + end);
      }
      return str;
    }
    
    0 讨论(0)
  • 2020-11-22 09:58

    Update 5 Nov 2016

    New answer, always accurate

    function toFixed(num, fixed) {
        var re = new RegExp('^-?\\d+(?:\.\\d{0,' + (fixed || -1) + '})?');
        return num.toString().match(re)[0];
    }
    

    As floating point math in javascript will always have edge cases, the previous solution will be accurate most of the time which is not good enough. There are some solutions to this like num.toPrecision, BigDecimal.js, and accounting.js. Yet, I believe that merely parsing the string will be the simplest and always accurate.

    Basing the update on the well written regex from the accepted answer by @Gumbo, this new toFixed function will always work as expected.


    Old answer, not always accurate.

    Roll your own toFixed function:

    function toFixed(num, fixed) {
        fixed = fixed || 0;
        fixed = Math.pow(10, fixed);
        return Math.floor(num * fixed) / fixed;
    }
    
    0 讨论(0)
  • 2020-11-22 09:58

    I used (num-0.05).toFixed(1) to get the second decimal floored.

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

    Building on David D's answer:

    function NumberFormat(num,n) {
      var num = (arguments[0] != null) ? arguments[0] : 0;
      var n = (arguments[1] != null) ? arguments[1] : 2;
      if(num > 0){
        num = String(num);
        if(num.indexOf('.') !== -1) {
          var numarr = num.split(".");
          if (numarr.length > 1) {
            if(n > 0){
              var temp = numarr[0] + ".";
              for(var i = 0; i < n; i++){
                if(i < numarr[1].length){
                  temp += numarr[1].charAt(i);
                }
              }
              num = Number(temp);
            }
          }
        }
      }
      return Number(num);
    }
    
    console.log('NumberFormat(123.85,2)',NumberFormat(123.85,2));
    console.log('NumberFormat(123.851,2)',NumberFormat(123.851,2));
    console.log('NumberFormat(0.85,2)',NumberFormat(0.85,2));
    console.log('NumberFormat(0.851,2)',NumberFormat(0.851,2));
    console.log('NumberFormat(0.85156,2)',NumberFormat(0.85156,2));
    console.log('NumberFormat(0.85156,4)',NumberFormat(0.85156,4));
    console.log('NumberFormat(0.85156,8)',NumberFormat(0.85156,8));
    console.log('NumberFormat(".85156",2)',NumberFormat(".85156",2));
    console.log('NumberFormat("0.85156",2)',NumberFormat("0.85156",2));
    console.log('NumberFormat("1005.85156",2)',NumberFormat("1005.85156",2));
    console.log('NumberFormat("0",2)',NumberFormat("0",2));
    console.log('NumberFormat("",2)',NumberFormat("",2));
    console.log('NumberFormat(85156,8)',NumberFormat(85156,8));
    console.log('NumberFormat("85156",2)',NumberFormat("85156",2));
    console.log('NumberFormat("85156.",2)',NumberFormat("85156.",2));
    
    // NumberFormat(123.85,2) 123.85
    // NumberFormat(123.851,2) 123.85
    // NumberFormat(0.85,2) 0.85
    // NumberFormat(0.851,2) 0.85
    // NumberFormat(0.85156,2) 0.85
    // NumberFormat(0.85156,4) 0.8515
    // NumberFormat(0.85156,8) 0.85156
    // NumberFormat(".85156",2) 0.85
    // NumberFormat("0.85156",2) 0.85
    // NumberFormat("1005.85156",2) 1005.85
    // NumberFormat("0",2) 0
    // NumberFormat("",2) 0
    // NumberFormat(85156,8) 85156
    // NumberFormat("85156",2) 85156
    // NumberFormat("85156.",2) 85156
    
    0 讨论(0)
  • It's more reliable to get two floating points without rounding.

    Reference Answer

    var number = 10.5859;
    var fixed2FloatPoints = parseInt(number * 100) / 100;
    console.log(fixed2FloatPoints);

    Thank You !

    0 讨论(0)
提交回复
热议问题