Decimal Rounding
These two pieces of code help you to rounded up, for you to display prices or orders are more useful:
Code 1:
Copy Code code as follows:
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;
}
/**
* usage:currencyformatted (12345.678);
* result:12345.68
**/
Code 2:
Copy Code code as follows:
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;
}
/**
* Usage:format_number (12345.678, 2);
* result:12345.68
**/
Add comma
These two pieces of code help you add commas to every three-digit number, which makes large numbers easier to see.
Code 1:
Copy Code code as follows:
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
**/
Code 2:
Copy Code code as follows:
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
**/
number formatting, from PHP
This JavaScript code feature is designed from PHP's Nubmer_format functionality. can be rounded and comma delimited. And you can customize the 10-binary partition.
Copy Code code as follows:
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
**/
Add a sort suffix in English
Copy Code code as follows:
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
*/
Remove characters that are not numeric
Copy Code code as follows:
function Stripnonnumeric (str)
{
str + = ';
var rgx =/^d|.| -$/;
var out = ';
for (var i = 0; i < str.length i++)
{
if (rgx.test (Str.charat (i))) {
if (!) ( (Str.charat (i) = = '. ' && out.indexof ('. ')!=-1) | |
(Str.charat (i) = = '-' && out.length!= 0)) {
Out + = Str.charat (i);
}
}
}
return out;
}
/*
* usage:stripnonnumeric (' 123et45dhs6.789 ');
* result:123456.789
*/