num-beauty
Version:
An ultra lightweight module for formatting numbers into human-friendly strings
1 lines • 658 B
JavaScript
export function round(e,a,r="HALF_UP"){if("bigint"==typeof e)return e;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),n=e<0,o=Math.abs(e),b=Math.round(o*t*1e10)/1e10,i=Math.floor(b),s=Math.ceil(b),u=b-i,c=Math.abs(u-.5)<1e-14,f=u>.5+1e-14;let h;switch(r){case"UP":h=s;break;case"DOWN":default:h=i;break;case"CEIL":h=n?i:s;break;case"FLOOR":h=n?s:i;break;case"HALF_UP":h=c||f?s:i;break;case"HALF_DOWN":h=f?s:i;break;case"HALF_EVEN":h=c?i%2==0?i:s:f?s:i}return Number(((n?-h:h)/t).toFixed(a))}