@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
33 return ( mod && mod . __esModule ) ? mod : { "default" : mod } ;
44} ;
55Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
6- exports . math = void 0 ;
6+ exports . math = exports . roundCustom = exports . RoundingMethod = void 0 ;
77exports . numberToPrecision = numberToPrecision ;
88/* eslint-disable */
99const mathjs_1 = __importDefault ( require ( "mathjs" ) ) ;
@@ -170,6 +170,39 @@ const combinationsFromIntervals = (arrA, arrB, arrC) => {
170170const roundValueToNDecimals = ( value , decimals = 3 ) => {
171171 return parseFloat ( value . toFixed ( decimals ) ) ;
172172} ;
173+ // See: https://en.wikipedia.org/wiki/Rounding
174+ var RoundingMethod ;
175+ ( function ( RoundingMethod ) {
176+ RoundingMethod [ "Bankers" ] = "bankers" ;
177+ RoundingMethod [ "HalfAwayFromZero" ] = "halfAwayFromZero" ;
178+ } ) ( RoundingMethod || ( exports . RoundingMethod = RoundingMethod = { } ) ) ;
179+ const roundCustom = ( value , decimals = 0 , method = RoundingMethod . HalfAwayFromZero ) => {
180+ const factor = Math . pow ( 10 , decimals ) ;
181+ const scaledValue = value * factor ;
182+ const absValue = Math . abs ( scaledValue ) ;
183+ const sign = scaledValue < 0 ? - 1 : 1 ;
184+ let roundedAbs ;
185+ switch ( method ) {
186+ case RoundingMethod . HalfAwayFromZero :
187+ roundedAbs = Math . round ( absValue ) ;
188+ break ;
189+ case RoundingMethod . Bankers :
190+ const floorValue = Math . floor ( absValue ) ;
191+ const fractional = absValue - floorValue ;
192+ if ( Math . abs ( fractional - 0.5 ) < Number . EPSILON ) {
193+ // Round to even
194+ roundedAbs = floorValue % 2 === 0 ? floorValue : floorValue + 1 ;
195+ }
196+ else {
197+ roundedAbs = Math . round ( absValue ) ;
198+ }
199+ break ;
200+ default :
201+ throw new Error ( `Unsupported rounding method: ${ method } ` ) ;
202+ }
203+ return ( roundedAbs * sign ) / factor ;
204+ } ;
205+ exports . roundCustom = roundCustom ;
173206/**
174207 * @summary Returns n splits of the passed segment.
175208 */
@@ -227,4 +260,6 @@ exports.math = {
227260 calculateSegmentsBetweenPoints3D,
228261 roundValueToNDecimals,
229262 numberToPrecision,
263+ roundCustom : exports . roundCustom ,
264+ RoundingMethod,
230265} ;
0 commit comments