我想最多四舍五入两位小数,但只有在必要时。
输入:
10
1.7777777
9.1
输出:
10
1.78
9.1
如何在JavaScript中执行此操作?
我想最多四舍五入两位小数,但只有在必要时。
输入:
10
1.7777777
9.1
输出:
10
1.78
9.1
如何在JavaScript中执行此操作?
当前回答
另一个简单的解决方案(无需编写任何函数)可以使用toFixed(),然后再次转换为float:
例如:
var objNumber = 1201203.1256546456;
objNumber = parseFloat(objNumber.toFixed(2))
其他回答
对这个答案稍作修改,似乎效果不错。
作用
function roundToStep(value, stepParam) {
var step = stepParam || 1.0;
var inv = 1.0 / step;
return Math.round(value * inv) / inv;
}
用法
roundToStep(2.55) = 3
roundToStep(2.55, 0.1) = 2.6
roundToStep(2.55, 0.01) = 2.55
与Brian Ustas建议的使用Math.round不同,我更喜欢Math.trunc方法来解决以下问题:
const twoDecimalRound = num => Math.round(num * 100) / 100;
const twoDecimalTrunc = num => Math.trunc(num * 100) / 100;
console.info(twoDecimalRound(79.996)); // Not desired output: 80;
console.info(twoDecimalTrunc(79.996)); // Desired output: 79.99;
const formattedNumber=数学舍入(数字*100)/100;
我的解决方案将输入视为字符串,并使用n位数字的“数学舍入”算法:取n位数字,如果数字n+1等于或大于5,则加1。它还允许指定负数,例如,将123.45舍入为-1的数字为120。它也适用于科学符号(例如1.2e-3)。我没有测量它的速度,我认为这不是最好的表现。
function safeRound( numInput, numPrecision ) {
const strNumber = numInput.toString().replace( 'E', 'e' );
const bSign = '+-'.indexOf( strNumber[ 0 ] ) !== -1;
const strSign = bSign ? strNumber[ 0 ] : '';
const numSign = strSign !== '-' ? +1 : -1;
const ixExponent = ( ixFound => ixFound !== -1 ? ixFound : strNumber.length )( strNumber.indexOf( 'e' ) );
const strExponent = strNumber.substr( ixExponent + 1 );
const numExponent = ixExponent !== strNumber.length ? Number.parseInt( strExponent ) : 0;
const ixDecimal = ( ixFound => ixFound !== -1 ? ixFound : ixExponent )( strNumber.indexOf( '.' ) );
const strInteger = strNumber.substring( !bSign ? 0 : 1, ixDecimal );
const strFraction = strNumber.substring( ixDecimal + 1, ixExponent );
const numPrecisionAdjusted = numPrecision + numExponent;
const strIntegerKeep = strInteger.substring( 0, strInteger.length + Math.min( 0, numPrecisionAdjusted ) ) + '0'.repeat( -Math.min( 0, numPrecisionAdjusted ) );
const strFractionKeep = strFraction.substring( 0, Math.max( 0, numPrecisionAdjusted ) );
const strRoundedDown = strSign + ( strIntegerKeep === '' ? '0' : strIntegerKeep ) + ( strFractionKeep === '' ? '' : '.' + strFractionKeep ) + ( strExponent === '' ? '' : 'e' + strExponent );
const chRoundUp = 0 <= numPrecisionAdjusted ? strFraction.substr( numPrecisionAdjusted, 1 ) : ( '0' + strInteger ).substr( numPrecisionAdjusted, 1 );
const bRoundUp = '5' <= chRoundUp && chRoundUp <= '9';
const numRoundUp = bRoundUp ? numSign * Math.pow( 10, -numPrecision ) : 0;
return Number.parseFloat( strRoundedDown ) + numRoundUp;
}
function safeRoundTest( numInput, numPrecision, strExpected ) {
const strActual = safeRound( numInput, numPrecision ).toString();
const bPassed = strActual === strExpected;
console.log( 'numInput', numInput, 'numPrecision', numPrecision, 'strExpected', strExpected, 'strActual', strActual, 'bPassed', bPassed );
return bPassed ? 0 : 1;
}
function safeRoundTests() {
let numFailed = 0;
numFailed += safeRoundTest( 0, 0, '0' );
numFailed += safeRoundTest( '0', 0, '0' );
numFailed += safeRoundTest( '0.1', 0, '0' );
numFailed += safeRoundTest( '+0.1', 0, '0' );
numFailed += safeRoundTest( '-0.1', 0, '0' );
numFailed += safeRoundTest( '0.1', 1, '0.1' );
numFailed += safeRoundTest( '+0.1', 1, '0.1' );
numFailed += safeRoundTest( '-0.1', 1, '-0.1' );
numFailed += safeRoundTest( '0.9', 0, '1' );
numFailed += safeRoundTest( '+0.9', 0, '1' );
numFailed += safeRoundTest( '-0.9', 0, '-1' );
numFailed += safeRoundTest( '0.9', 1, '0.9' );
numFailed += safeRoundTest( '+0.9', 1, '0.9' );
numFailed += safeRoundTest( '-0.9', 1, '-0.9' );
numFailed += safeRoundTest( '0.5', 0, '1' );
numFailed += safeRoundTest( '+0.5', 0, '1' );
numFailed += safeRoundTest( '-0.5', 0, '-1' );
numFailed += safeRoundTest( '0.4999', 0, '0' );
numFailed += safeRoundTest( '+0.4999', 0, '0' );
numFailed += safeRoundTest( '-0.4999', 0, '0' );
numFailed += safeRoundTest( '1.005', 2, '1.01' );
numFailed += safeRoundTest( '1.00499999999', 2, '1' );
numFailed += safeRoundTest( '012.3456', -4, '0' );
numFailed += safeRoundTest( '012.3456', -3, '0' );
numFailed += safeRoundTest( '012.3456', -2, '0' );
numFailed += safeRoundTest( '012.3456', -1, '10' );
numFailed += safeRoundTest( '012.3456', 0, '12' );
numFailed += safeRoundTest( '012.3456', 1, '12.3' );
numFailed += safeRoundTest( '012.3456', 2, '12.35' );
numFailed += safeRoundTest( '012.3456', 3, '12.346' );
numFailed += safeRoundTest( '012.3456', 4, '12.3456' );
numFailed += safeRoundTest( '012.3456', 5, '12.3456' );
numFailed += safeRoundTest( '12.', 0, '12' );
numFailed += safeRoundTest( '.12', 2, '0.12' );
numFailed += safeRoundTest( '0e0', 0, '0' );
numFailed += safeRoundTest( '1.2e3', 0, '1200' );
numFailed += safeRoundTest( '1.2e+3', 0, '1200' );
numFailed += safeRoundTest( '1.2e-3', 0, '0' );
numFailed += safeRoundTest( '1.2e-3', 3, '0.001' );
numFailed += safeRoundTest( '1.2e-3', 4, '0.0012' );
numFailed += safeRoundTest( '1.2e-3', 5, '0.0012' );
numFailed += safeRoundTest( '+12.', 0, '12' );
numFailed += safeRoundTest( '+.12', 2, '0.12' );
numFailed += safeRoundTest( '+0e0', 0, '0' );
numFailed += safeRoundTest( '+1.2e3', 0, '1200' );
numFailed += safeRoundTest( '+1.2e+3', 0, '1200' );
numFailed += safeRoundTest( '+1.2e-3', 0, '0' );
numFailed += safeRoundTest( '+1.2e-3', 3, '0.001' );
numFailed += safeRoundTest( '+1.2e-3', 4, '0.0012' );
numFailed += safeRoundTest( '+1.2e-3', 5, '0.0012' );
numFailed += safeRoundTest( '-12.', 0, '-12' );
numFailed += safeRoundTest( '-.12', 2, '-0.12' );
numFailed += safeRoundTest( '-0e0', 0, '0' );
numFailed += safeRoundTest( '-1.2e3', 0, '-1200' );
numFailed += safeRoundTest( '-1.2e+3', 0, '-1200' );
numFailed += safeRoundTest( '-1.2e-3', 0, '0' );
numFailed += safeRoundTest( '-1.2e-3', 3, '-0.001' );
numFailed += safeRoundTest( '-1.2e-3', 4, '-0.0012' );
numFailed += safeRoundTest( '-1.2e-3', 5, '-0.0012' );
numFailed += safeRoundTest( '9876.543e210', 0, '9.876543e+213' );
numFailed += safeRoundTest( '9876.543e210', -210, '9.877e+213' );
numFailed += safeRoundTest( '9876.543e210', -209, '9.8765e+213' );
numFailed += safeRoundTest( '9876.543e+210', 0, '9.876543e+213' );
numFailed += safeRoundTest( '9876.543e+210', -210, '9.877e+213' );
numFailed += safeRoundTest( '9876.543e+210', -209, '9.8765e+213' );
numFailed += safeRoundTest( '9876.543e-210', 213, '9.876543e-207' );
numFailed += safeRoundTest( '9876.543e-210', 210, '9.877e-207' );
numFailed += safeRoundTest( '9876.543e-210', 211, '9.8765e-207' );
console.log( 'numFailed', numFailed );
}
safeRoundTests();
它可能对你有用,
Math.round(num * 100)/100;
了解toFixed和round之间的区别。您可以查看Math.round(num)vs num.toFixed(0)和浏览器不一致性。