Javascript does not have any built-in formatting functions. Here we collect 5 javascript digital formatting functions through Google, hoping to bring convenience to your web development.
Rounding in decimal format
These two sections of code help you round the list and show the price or order more useful for you:
Code 1:
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:
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 (10, (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 a comma to each three digits, which makes it easier to view large numbers.
Code 1:
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:
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, '$ 1' +', '+' $2 ');
}
Return x1 + x2;
}
/**
* Usage: addCommas (12345678 );
* Result: 12,345,678
**/
Number formatting, from PHP
This javascript code is designed with nubmer_format from PHP. Can be rounded to and separated by commas. In addition, you can customize the hexadecimal separation.
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 (10, 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 an English sort suffix
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 % 10> 4? Suffix [0]: suffix [n % 10]);
Return this + ord;
}
/*
* Usage:
* Var myNumOld = 23
* Var myNumNew = myNumOld. toOrdinal ()
* Result: 23rd
*/
Remove non-numeric characters
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. 6379 ');
* Result: 123456.789
*/
Hope to help you!
Original article from: Share 5 javascript numeric formatting Functions