JS to implement format amount, character, time method _javascript skill

Source: Internet
Author: User

The example of this article describes the format of JS to achieve the amount, character, time method. Share to everyone for your reference. The implementation methods are as follows:

Copy Code code as follows:
Convert amount to uppercase
function Todax (num) {
Amount Capitalization Conversion
if (isNaN (num) | | | num > Math.pow (10, 12))
Return "";
var cn = "0 Woolu qi ba Nine";
var unit = new Array ("Collect thousand", "angle");
var unit1 = new Array ("trillion", "");
if (parsefloat (num) ==0| | num== ') {
Return "0 yuan";
}
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 ($) {
Return Cn.charat ($) + 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 (/(0.) +/g, "0"). Replace (/0 +$/, "");
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] + "Yuan": numarray[0],numarray[1] = Numarray[1].replace (/^ 0 +/, "");
NUMARRAY[1] = numarray[1].match (/min/)? NUMARRAY[1]: numarray[1] + "whole";
return numarray[0] + numarray[1];
}
/** amount Format Add "," Separator * *

Function Addcomma (money) {
if (money== "") {
return "";
}
if (money) {
Money = Money.trim ();
}
    if (/[^0-9\.\-\+]/.test) {  
    return;
 & nbsp; }
    money = parsefloat ($) + "; 
    if (' NaN ' = money) {
    return  "0.00";
   }
    var money_flag = "";
    if (Money.indexof ("-")!=-1) {
    money = Money.replace (/-/g, "");
     Money_flag = "-";
   }

Money=money.replace (/^ (\d*) $/, "$");
Money= (money+). Replace (/(\d*\.\d\d) \d*/, "$");
Money=money.replace (".", ",");
var re=/(\d) (\d{3},)/;
while (Re.test) {
Money=money.replace (Re, "$1,$2");
}
Money=money.replace (/, (\d\d) $/, ". $");
var money = money_flag+ "" +money.replace (/^\./, "0.")
return money;
}

/** amount formatted to "," separator * *
function Delcomma (value) {
var rtnval = value + "";
Return Rtnval.replace (/,/g, "");
}

/**
* Amount format string Add decimal point
*/
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);
}
/**
* Amount format number to decimal point
*/
function Removepoint (Money) {
if (/[^0-9\.] /.test (Money)) {
return money;
}

var valuefloat = parsefloat (money) * 100;
var valueint = parseint (valuefloat);
return valueint;
}
/* Format two digits after the decimal point shown as a percentage * *
function Addpercent (str) {
var percent = Math.floor (str * 100)/100;
Percent= (percent.tofixed (2));
Return percent+ '% ';
}

/** character formatting Add space separator * *
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;
}

/** character formatting Remove space Separator * *
function Removespace (value) {
var rtnval = value + "";
return Rtnval.replace (//g, "");
}

Format Date 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 with your JavaScript programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.