1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| num.toFixed(2)
Math.round(num * 100) / 100;
Math.round((num + Number.EPSILON) * 100) / 100;
Number(Math.round(num + 'e2') + 'e-2')
function roundByString(num, decimals = 2) { if (typeof num !== 'number' || isNaN(num)) { return NaN; }
const isNegative = num < 0; const absStr = String(Math.abs(num)); let [integerPart, decimalPart = ''] = absStr.split('.');
decimalPart = (decimalPart + '0'.repeat(decimals + 1)).slice(0, decimals + 1);
let combined = integerPart + decimalPart;
const dotIndex = combined.length - 1; const numberWithDecimal = combined.slice(0, dotIndex) + '.' + combined.slice(dotIndex); let numberToRound = Number(numberWithDecimal);
numberToRound = Math.round(numberToRound);
let resultStr = String(numberToRound).padStart(integerPart.length + decimals, '0'); const newInteger = resultStr.slice(0, -decimals) || '0'; const newDecimal = resultStr.slice(-decimals).padEnd(decimals, '0');
const finalStr = (isNegative ? '-' : '') + newInteger + '.' + newDecimal; return Number(finalStr); }
function toFixedTrunc(num, decimals = 2) { if (typeof num !== 'number' || isNaN(num)) { return '0.' + '0'.repeat(decimals); } const [integer, decimal = ''] = String(num).split('.');
const fixedDecimal = (decimal + '0'.repeat(decimals)).slice(0, decimals); return `${integer}.${fixedDecimal}`; }
new Big(a).toFixed(2, 0)
Number(num + 'e2') + '%'
|