UNPKG

num-beauty

Version:

An ultra lightweight module for formatting numbers into human-friendly strings

1 lines 627 B
export function round(e,a,r="HALF_UP"){if("number"!=typeof e||isNaN(e))throw new Error("Number must be a valid numeric value");if(!Number.isInteger(a)||a<0)throw new Error("Number of decimal places must be a non-negative integer");const t=Math.pow(10,a),o=e<0,s=Math.abs(e),b=Math.round(s*t*1e10)/1e10,n=Math.floor(b),c=Math.ceil(b),u=b-n,i=Math.abs(u-.5)<1e-14,h=u>.5+1e-14;let m;switch(r){case"UP":m=c;break;case"DOWN":default:m=n;break;case"CEIL":m=o?n:c;break;case"FLOOR":m=o?c:n;break;case"HALF_UP":m=i||h?c:n;break;case"HALF_DOWN":m=h?c:n;break;case"HALF_EVEN":m=i?n%2==0?n:c:h?c:n}return Number(((o?-m:m)/t).toFixed(a))}