Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var n = { // Object to store everything in, this isn't required but helps it take up less space
- log: Math.log(10), // Math.log(10) is stored here to prevent the need to recalculate it multiple times
- array: ["", "k", "M", "B", "T", "Qa", "Qi", "Sx", "Sp", "Oc", "No", "Dc", "UnD", "DuD", "TrD", "QaD", "QiD", "SeD", "SpD", "OcD", "NoD", "Vi", "UnV"], // This array can go on as long as you need, it's the abbreviations for large numbers
- floor: function(a) { // This function is Math.floor() only with support for floating points and negative numbers
- // a (required) = number to round down to whole numbers
- if(Math.abs(Math.abs(a) - Math.abs(Math.floor(a))) >= 0.999999991) {
- if(a >= 0) {
- return Math.ceil(a);
- }else{
- return Math.floor(a);
- }
- }else{
- if(a >= 0) {
- return Math.floor(a);
- }else{
- return Math.ceil(a);
- }
- }
- },
- format: function(a, b) { // This is the actual number formatting function, also supports floating points and negative numbers. If number is greater than array of long number endings supports than "Infinite" is returned.
- // a (required) = number to format
- // b (optional) = number of decimal places to use for numbers under 1000 (ones without letter endings)
- var l, p, r;
- if(n.floor(Math.log(Math.abs(a)) / n.log) <= 0) {
- l = 0;
- }else{
- l = n.floor(Math.log(Math.abs(a)) / n.log;
- }
- if(l % 3 === 0) {
- p = 2;
- }else if((l - 1) % 3 === 0) {
- p = 1;
- }else{
- p = 0;
- }
- if(Math.abs(a) < 1000) {
- if(typeof b === "number") {
- r = a.toFixed(b);
- }else{
- r = n.floor(a);
- }
- }else{
- r = n.floor(a / (Math.pow(10, n.floor(l / 3) * 3 - p))) / Math.pow(10, p);
- }
- if(typeof n.array[n.floor(l / 3)] === "string") {
- return r + n.array[n.floor(l / 3)];
- }else{
- return "Infinite";
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment