numer.js
Version:
An open-source JavaScript library for formatting and manipulating numbers.
1 lines • 848 B
JavaScript
!function(t,e){"function"==typeof define&&define.amd?define([],e):"object"==typeof module&&"undefined"!=typeof exports?module.exports=e():t.Numer=e()}(this,function(){"use strict";function t(t){this._input=t}return t.prototype={format:function(t){var e,r,n,o=t;switch(this._input.style){case"comma":return o.toString().replace(/\B(?=(\d{3})+(?!\d))/g,",");case"abbreviation":return function(t){var e=t;if(1e3<=t){for(var r=Math.floor((""+t).length/3),n="",o=2;1<=o;o--)if(((n=parseFloat((0!==r?t/Math.pow(1e3,r):t).toPrecision(o)))+"").replace(/[^a-zA-Z 0-9]+/g,"").length<=2)break;n%1!=0&&(n=n.toFixed(1)),e=n+["","K","M","B","T"][r]}return e}(o);case"ordinal":return n=(e=o)%100,1!=(r=e%10)||11==n?2!=r||12==n?3!=r||13==n?e+"th":e+"rd":e+"nd":e+"st";default:throw new Error("The formatting style to use: comma, abbreviation and ordinal.")}}},t});