This article describes how to format the amount, characters, and time in javascript. Examples of common javascript conversion techniques such as amount, character, and date are analyzed, which has some reference value, for more information about how to format the amount, characters, and time in JavaScript, see the examples in this article. Share it with you for your reference. The specific implementation method is as follows:
The code is as follows:
// Convert the amount to uppercase
Function toDaX (num ){
// Case-sensitive conversion of the amount
If (isNaN (num) | num> Math. pow (10, 12 ))
Return "";
Var cn = "";
Var unit = new Array ("qibai", "points ");
Var unit1 = new Array ("trillion ","");
If (parseFloat (num) = 0 | num = ''){
Return "zero circle ";
}
Var numArray = num. toString (). split (".");
Var start = new Array (numArray [0]. length-1, 2 );
Function toChinese (num, index ){
Var num = num. replace (/\ d/g, function ($1 ){
Return cn. charAt ($1) + unit [index]. charAt (start -- % 4? Start % 4:-1 );
});
Return num;
}
For (var I = 0; I <numArray. length; I ++ ){
Var tmp = "";
For (var j = 0; j * 4 <numArray [I]. length; j ++ ){
Var strIndex = numArray [I]. length-(j + 1) * 4;
Var str = numArray [I]. substring (strIndex, strIndex + 4 );
Var start = I? 2: str. length-1;
Var tmp1 = toChinese (str, I );
Tmp1 = tmp1.replace (/(zero.) +/g, "zero"). replace (/Zero + $ /,"");
Tmp1 = tmp1.replace (/^ pick up/, "pick up ");
Tmp = (tmp1 + unit1 [I]. charAt (j-1) + tmp;
}
NumArray [I] = tmp;
}
NumArray [1] = numArray [1]? NumArray [1]: "";
NumArray [0] = numArray [0]? NumArray [0] + "RMB": numArray [0], numArray [1] = numArray [1]. replace (/^ Zero + /,"");
NumArray [1] = numArray [1]. match (/minute /)? NumArray [1]: numArray [1] + "integer ";
Return numArray [0] + numArray [1];
}
/** Add the "," separator for formatting the amount */
Function addComma (money ){
If (money = ""){
Return "";
}
If (money ){
Money = money. trim ();
}
If (/[^ 0-9 \. \-\ +]/. test (money )){
Return money;
}
Money = parseFloat (money) + "";
If ('Nan '= money ){
Return "0.00 ";
}
Var money_flag = "";
If (money. indexOf ("-")! =-1 ){
Money = money. replace (/-/g ,"");
Money_flag = "-";
}
Money = money. replace (/^ (\ d *) $/, "$1 .");
Money = (money + "00"). replace (/(\ d * \. \ d) \ d */, "$1 ");
Money = money. replace (".",",");
Var re =/(\ d) (\ d {3 },)/;
While (re. test (money )){
Money = money. replace (re, "$1, $2 ");
}
Money = money. replace (/, (\ d) $/, ". $1 ");
Var money = money_flag + "" + money. replace (/^ \./, "0 .")
Return money;
}
/** Format the amount to "," separator */
Function delComma (value ){
Var rtnVal = value + "";
Return rtnVal. replace (/,/g ,"");
}
/**
* Add a decimal point to an amount formatted string
*/
Function addPoint (money ){
If (/[^ 0-9 \.]/. test (money )){
Return money;
}
If (money. length <3 | money. indexOf (".")>-1 ){
Return money;
}
Return money. substring (0, money. length-2) + "." + money. substring (money. length-2, money. length );
}
/**
* Formatting the amount to decimal places
*/
Function removePoint (money ){
If (/[^ 0-9 \.]/. test (money )){
Return money;
}
Var valueFloat = parseFloat (money) * 100;
Var valueInt = parseInt (valueFloat );
Return valueInt;
}
/* Format the two digits after the decimal point to display in percentage */
Function addPercent (str ){
Var percent = Math. floor (str * 100)/100;
Percent = (percent. toFixed (2 ));
Return percent + '% ';
}
/** Add a space separator for character formatting */
Function addSpace (value ){
If (value = null | value = ""){
Return "";
}
Var value = value + "";
Var tmpStr = "";
While (value. length> 4 ){
TmpStr = tmpStr + value. substring (0, 4) + "";
Value = value. substring (4, value. length );
}
TmpStr = tmpStr + value;
Return tmpStr;
}
/** Remove space separators for character formatting */
Function removeSpace (value ){
Var rtnVal = value + "";
Return rtnVal. replace (// g ,"");
}
// Format the date and time string
// YYYYMMDD-YYYY-MM-DD
// YYYYMMDDhhmmss-, YYYY-MM-DD hh: mm: ss
Function formatDatetime (oldvalue ){
If (oldvalue = null ){
Return "";
} Else if (oldvalue. length = 8 ){
Return oldvalue. substring (0, 4) +
"-" + Oldvalue. substring (4, 6) +
"-" + Oldvalue. substring (6, 8 );
} Else if (oldvalue. length = 14 ){
Return oldvalue. substring (0, 4) +
"-" + Oldvalue. substring (4, 6) +
"-" + Oldvalue. substring (6, 8) +
"" + Oldvalue. substring (8, 10) +
":" + Oldvalue. substring (10, 12) +
":" + Oldvalue. substring (12, 14 );
} Else if (oldvalue. length = 6 ){
Return oldvalue. substring (0, 2) +
":" + Oldvalue. substring (2, 4) +
":" + Oldvalue. substring (4, 6 );
} Else {
Return oldvalue;
}
}
Function StringToDate (str ){
Var datainfo = str. split ('-');
Return new Date (datainfo [0], datainfo [1], datainfo [2]);
}
I hope this article will help you design javascript programs.