我想最多四舍五入两位小数,但只有在必要时。
输入:
10
1.7777777
9.1
输出:
10
1.78
9.1
如何在JavaScript中执行此操作?
我想最多四舍五入两位小数,但只有在必要时。
输入:
10
1.7777777
9.1
输出:
10
1.78
9.1
如何在JavaScript中执行此操作?
当前回答
将类型保留为整数,以便以后进行排序或其他算术运算:
Math.round(1.7777777 * 100)/100
1.78
// Round up!
Math.ceil(1.7777777 * 100)/100
1.78
// Round down!
Math.floor(1.7777777 * 100)/100
1.77
或转换为字符串:
(1.7777777).toFixed(2)
"1.77"
其他回答
我正在构建一个简单的tipCalculator,这里有很多答案似乎使问题过于复杂。所以我发现总结这个问题是真正回答这个问题的最佳方式。
如果要创建舍入的十进制数,首先调用Fixed(要保留的小数位数),然后将其包装在number()中。
最终结果是:
let amountDue = 286.44;
tip = Number((amountDue * 0.2).toFixed(2));
console.log(tip) // 57.29 instead of 57.288
舍入问题可以通过使用指数表示法来避免。
public roundFinancial(amount: number, decimals: number) {
return Number(Math.round(Number(`${amount}e${decimals}`)) + `e-${decimals}`);
}
const formattedNumber=数学舍入(数字*100)/100;
这是astorije的答案的修改版本,更好地支持负值舍入。
// https://stackoverflow.com/a/21323513/384884
// Modified answer from astorije
function round(value, precision) {
// Ensure precision exists
if (typeof precision === "undefined" || +precision === 0) {
// Just do a regular Math.round
return Math.round(value);
}
// Convert the value and precision variables both to numbers
value = +value;
precision = +precision;
// Ensure the value is a number and that precision is usable
if (isNaN(value) || !(typeof precision === "number" && precision % 1 === 0)) {
// Return NaN
return NaN;
}
// Get the sign of value
var signValue = Math.sign(value);
// Get the absolute value of value
value = Math.abs(value);
// Shift
value = value.toString().split("e");
value = Math.round(+(value[0] + "e" + (value[1] ? (+value[1] + precision) : precision)));
// Shift back
value = value.toString().split("e");
value = +(value[0] + "e" + (value[1] ? (+value[1] - precision) : -precision));
// Apply the sign
value = value * signValue;
// Return rounded value
return value;
}
我的解决方案将输入视为字符串,并使用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();