16 lines
573 B
TypeScript
16 lines
573 B
TypeScript
/**
|
|
* Round half away from zero ('commercial' rounding)
|
|
* Uses correction to offset floating-point inaccuracies.
|
|
* Works symmetrically for positive and negative numbers.
|
|
*/
|
|
export function round(num : number, decimalPlaces : number = 0) : number {
|
|
const p = Math.pow(10, decimalPlaces);
|
|
const n = (num * p) * (1 + Number.EPSILON);
|
|
return Math.round(n) / p;
|
|
}
|
|
|
|
export function floor(num : number, decimalPlaces : number = 0) : number {
|
|
const p = Math.pow(10, decimalPlaces);
|
|
const n = (num * p) * (1 + Number.EPSILON);
|
|
return Math.floor(n) / p;
|
|
} |