这一行代码把数字四舍五入到小数点后两位。但我得到的数字是这样的:10.8、2.4等等。这些都不是我的小数点后两位的想法,所以我怎么能改善以下?

Math.round(price*Math.pow(10,2))/Math.pow(10,2);

我想要10.80、2.40等数字。jQuery的使用对我来说很好。


当前回答

四舍五入您的十进制值,然后使用toFixed(x)为您期望的数字(s)。

function parseDecimalRoundAndFixed(num,dec){
  var d =  Math.pow(10,dec);
  return (Math.round(num * d) / d).toFixed(dec);
}

Call

parseDecimalRoundAndFixed(10.800243929,4) => 10.80 parseDecimalRoundAndFixed(10.807243929,2) => 10.81

其他回答

下面是https://stackoverflow.com/a/21323330/916734的TypeScript实现。它还使用了函数,并允许可选的数字偏移量。

export function round(rawValue: number | string, precision = 0, fractionDigitOffset = 0): number | string {
  const value = Number(rawValue);
  if (isNaN(value)) return rawValue;

  precision = Number(precision);
  if (precision % 1 !== 0) return NaN;

  let [ stringValue, exponent ] = scientificNotationToParts(value);

  let shiftExponent = exponentForPrecision(exponent, precision, Shift.Right);
  const enlargedValue = toScientificNotation(stringValue, shiftExponent);
  const roundedValue = Math.round(enlargedValue);

  [ stringValue, exponent ] = scientificNotationToParts(roundedValue);
  const precisionWithOffset = precision + fractionDigitOffset;
  shiftExponent = exponentForPrecision(exponent, precisionWithOffset, Shift.Left);

  return toScientificNotation(stringValue, shiftExponent);
}

enum Shift {
  Left = -1,
  Right = 1,
}

function scientificNotationToParts(value: number): Array<string> {
  const [ stringValue, exponent ] = value.toString().split('e');
  return [ stringValue, exponent ];
}

function exponentForPrecision(exponent: string, precision: number, shift: Shift): number {
  precision = shift * precision;
  return exponent ? (Number(exponent) + precision) : precision;
}

function toScientificNotation(value: string, exponent: number): number {
  return Number(`${value}e${exponent}`);
}

@heridev和我用jQuery创建了一个小函数。

接下来你可以试试:

HTML

<input type="text" name="one" class="two-digits"><br>
<input type="text" name="two" class="two-digits">​

jQuery

// apply the two-digits behaviour to elements with 'two-digits' as their class
$( function() {
    $('.two-digits').keyup(function(){
        if($(this).val().indexOf('.')!=-1){         
            if($(this).val().split(".")[1].length > 2){                
                if( isNaN( parseFloat( this.value ) ) ) return;
                this.value = parseFloat(this.value).toFixed(2);
            }  
         }            
         return this; //for chaining
    });
});

​ 在线演示:

http://jsfiddle.net/c4Wqn/

通常,十进制舍入是通过缩放完成的:round(num * p) / p

天真的实现

使用下面带有中间数的函数,您将得到预期的上四舍五入值,或者有时根据输入得到下四舍五入值。

这种舍入不一致可能会在客户端代码中引入难以检测的错误。

函数naiveRound(num, decimalPlaces) { var p =数学。decimalPlaces战俘(10日); 返回数学。Round (num * p) / p; } console.log(naiveRound(1.245, 2));// 1.25 correct(四舍五入) console.log(naiveRound(1.255, 2));// 1.25不正确(应该是1.26)

更好的实现

通过将数字转换为指数表示法中的字符串,正数将按预期四舍五入。 但是要注意,负数四舍五入和正数四舍五入是不同的。

事实上,它的执行基本上相当于“四舍五入”的规则,您将看到round(-1.005, 2)的计算结果为-1,即使round(1.005, 2)的计算结果为1.01。lodash _。圆法使用这种技术。

/** * Round half up ('round half towards positive infinity') * Uses exponential notation to avoid floating-point issues. * Negative numbers round differently than positive numbers. */ function round(num, decimalPlaces) { num = Math.round(num + "e" + decimalPlaces); return Number(num + "e" + -decimalPlaces); } // test rounding of half console.log( round(0.5, 0) ); // 1 console.log( round(-0.5, 0) ); // 0 // testing edge cases console.log( round(1.005, 2) ); // 1.01 console.log( round(2.175, 2) ); // 2.18 console.log( round(5.015, 2) ); // 5.02 console.log( round(-1.005, 2) ); // -1 console.log( round(-2.175, 2) ); // -2.17 console.log( round(-5.015, 2) ); // -5.01

如果希望在负数舍入时保持通常的行为,则需要在调用Math.round()之前将负数转换为正数,然后在返回之前将它们转换回负数。

// Round half away from zero
function round(num, decimalPlaces) {
    num = Math.round(Math.abs(num) + "e" + decimalPlaces) * Math.sign(num);
    return Number(num + "e" + -decimalPlaces);
}

有一种不同的纯数学技术来执行舍入到最近(使用“舍入到离零一半远的地方”),在调用舍入函数之前应用epsilon校正。

简单地说,我们添加最小的浮动值(= 1.0 ulp;单位在最后的位置)到数字之前舍入。这将移动到数字之后的下一个可表示的值,远离零。

/** * Round half away from zero ('commercial' rounding) * Uses correction to offset floating-point inaccuracies. * Works symmetrically for positive and negative numbers. */ function round(num, decimalPlaces) { var p = Math.pow(10, decimalPlaces); var e = Number.EPSILON * num * p; return Math.round((num * p) + e) / p; } // test rounding of half console.log( round(0.5, 0) ); // 1 console.log( round(-0.5, 0) ); // -1 // testing edge cases console.log( round(1.005, 2) ); // 1.01 console.log( round(2.175, 2) ); // 2.18 console.log( round(5.015, 2) ); // 5.02 console.log( round(-1.005, 2) ); // -1.01 console.log( round(-2.175, 2) ); // -2.18 console.log( round(-5.015, 2) ); // -5.02

这是为了抵消十进制数编码过程中可能出现的隐式舍入误差,特别是十进制数的最后一个位置有“5”的数字,如1.005、2.675和16.235。实际上,十进制系统中的1.005在64位二进制浮点数中被编码为1.0049999999999999;而十进制系统中的1234567.005被编码为64位二进制浮点的1234567.0049999998882413。

值得注意的是,二进制舍入误差的最大值取决于(1)数字的大小和(2)相对机器的ε(2^-52)。

要使用定点表示法格式化一个数字,你可以简单地使用toFixed方法:

(10.8).toFixed(2); // "10.80"

var num = 2.4;
alert(num.toFixed(2)); // "2.40"

注意,toFixed()返回一个字符串。

注意:toFixed在90%的情况下不会四舍五入,它会返回四舍五入的值,但在很多情况下,它不起作用。

例如:

2.005.toFixed(2) === "2.00"

更新:

现在,你可以使用国际电话。NumberFormat构造函数。它是ECMAScript国际化API规范(ECMA402)的一部分。它有很好的浏览器支持,甚至包括IE11,并且Node.js完全支持它。

const formatter = new Intl。NumberFormat(“en - us”{ minimumFractionDigits: 2 maximumFractionDigits: 2 }); console.log (formatter.format (2.005));/ /“2.01” console.log (formatter.format (1.345));/ /“1.35”

你也可以使用toLocaleString方法,它会在内部使用Intl API:

const format = (num, decimals) => num. tolocalestring ('en-US', { minimumFractionDigits: 2 maximumFractionDigits: 2 }); console.log(格式(2.005));/ /“2.01” console.log(格式(1.345));/ /“1.35”

这个API还为您提供了各种格式选项,如千个分隔符、货币符号等。

Number(Math.round(1.005+'e2')+'e-2'); // 1.01

这对我很有用:在JavaScript中舍入小数