1. Rounding
The following two functions can be rounded to the floating-point number, reserving two digits after the decimal point.
/** the following two functions can be rounded to the floating-point number, retaining two digits after the decimal point **/
| The code is as follows |
Copy Code |
| function currencyformatted (amount) { var i = parsefloat (amount); if (isNaN (i)) {i = 0.00;} var minus = '; if (I < 0) {minus = '-';} i = Math.Abs (i); i = parseint ((i +. 005) * 100); i = i/100; s = new String (i); if (S.indexof ('. ') < 0) {s + = '. 00 ';} if (S.indexof ('. ') = = (S.length-2)) {s + = ' 0 ';} s = minus + S; return s; } function Format_number (pnumber,decimals) { if (isNaN (Pnumber)) {return 0}; if (pnumber== ') {return 0}; var snum = new String (pnumber); var sec = Snum.split ('. '); var whole = parsefloat (sec[0]); var result = '; if (Sec.length > 1) { var dec = new String (sec[1]); Dec = String (parsefloat (sec[1))/math.pow ((dec.length-decimals)); Dec = String (whole + math.round (parsefloat (DEC))/math.pow (10,decimals)); var dot = dec.indexof ('. '); if (dot = = 1) { Dec + = '. '; Dot = Dec.indexof ('. '); } while (dec.length <= dot + decimals) {Dec = ' 0 ';} result = Dec; } else{ var dot; var dec = new String (whole); Dec + = '. '; Dot = Dec.indexof ('. '); while (dec.length <= dot + decimals) {Dec = ' 0 ';} result = Dec; } return result; }
|
2. Add commas to numbers
Add a comma per three digits to facilitate reading
/** Add a comma per three digits to facilitate reading **/
| The code is as follows |
Copy Code |
| function commaformatted (amount) { var delimiter = ","; Replace comma if desired Amount = new String (amount); var a = Amount.split ('. ', 2) var d = a[1]; var i = parseint (a[0]); if (isNaN (i)) {return ';} var minus = '; if (I < 0) {minus = '-';} i = Math.Abs (i); var n = new String (i); var a = []; while (N.length > 3) { var nn = n.substr (n.length-3); A.unshift (NN); n = n.substr (0,n.length-3); } if (N.length > 0) {a.unshift (n);} n = a.join (delimiter); if (D.length < 1) {amount = n;} else {amount = n + '. ' + D;} Amount = minus + amount; return amount; } /** * usage:commaformatted (12345678); * result:12,345,678 **/ function Addcommas (NSTR) { Nstr + = '; var x = Nstr.split ('. '); var x1 = x[0]; var x2 = x.length >; 1? '. ' + x[1]: '; var rgx =/(d+) (d{3})/; while (Rgx.test (x1)) { X1 = X1.replace (RGX, ' $ ' + ', ' + ' $ '); } return x1 + x2; } /** * Usage:addcommas (12345678); * result:12,345,678 **/ |
3. PHP-like Number_format function
This function can be added to separate commas or rounded.
/** This function can be added to separate commas or rounded. **/
| The code is as follows |
Copy Code |
| function Number_format (number, decimals, Dec_point, thousands_sep) { Number = (number + '). Replace (/[^0-9+-ee.] /g, ""); var n =!isfinite (+number)? 0: +number, Prec =!isfinite (+decimals)? 0:math.abs (decimals), Sep = (typeof thousands_sep = = ' undefined ')? ', ': Thousands_sep, Dec = (typeof dec_point = = ' undefined ')? '. ': Dec_point, s = ', Tofixedfix = function (n, prec) { var k = Math.pow (PREC); Return ' + Math.Round (n * k)/k; }; Fix for IE parsefloat (0.55). toFixed (0) = 0; s = (Prec tofixedfix (n, prec): ' + Math.Round (n)). Split ('. ') if (S[0].length > 3) { S[0] = S[0].replace (/b (? = (?:d {3}) + (?! d)/g, Sep); } if ((s[1) | | '). Length < Prec) { S[1] = S[1] | | ''; S[1] + = new Array (prec-s[1].length + 1). Join (' 0 '); } Return S.join (DEC); } /** * Usage:number_format (123456.789, 2, '. ', ', '); * result:123,456.79 **/ |
4. Add ordinal words
Add the ordinal numbers "St, ND, RD, TH" to the number.
/** adds the ordinal number of "St, ND, RD, TH" to the numbers. **/
| The code is as follows |
Copy Code |
Number.prototype.toOrdinal = function () { var n = this% 100; var suffix = [' th ', ' st ', ' nd ', ' rd ', ' th ']; var ord = n < 21? (N < 4 Suffix[n]: suffix[0]): (n% > 4? suffix[0]: suffix[n% 10]); return this + ord; } /*
* Usage: * var mynumold = 23 * var mynumnew = mynumold.toordinal () * result:23rd */ |
To sum up, the rounding function is like JS's own function
Seemingly rounded with the tofixed can be achieved.
| code is as follows |
copy code |
| (2.83). ToFixed (0) ==> 3 (2.6666). toFixed (2) ==> 2.67 |