UNPKG

money-lib

Version:

TypeScript library to work with money

21 lines (20 loc) 721 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.roundBank = void 0; /** * "Banker's Rounding" - to minimize bias */ const roundBank = (n, places = 0) => { const factor = 10 ** places; const roundedNumber = n * factor; const roundedDown = Math.floor(roundedNumber); const roundedUp = Math.ceil(roundedNumber); const decimalPart = roundedNumber - roundedDown; const isHalfway = Math.abs(decimalPart - 0.5) < Number.EPSILON; if (isHalfway) { const isLeftDigitEven = roundedDown % 2 === 0; return isLeftDigitEven ? roundedDown / factor : roundedUp / factor; } return Math.round(n * factor) / factor; }; exports.roundBank = roundBank;