Javascript code _ javascript tips-js tutorial

Source: Internet
Author: User
If you want to display the currency data in a certain form on the page, you must first format it. The following describes the specific formatting code. If you are interested, refer The Code is as follows:


Function formatCurrency (num ){
Var sign = "";
If (isNaN (num ))
{
Num = 0;
}
If (num <0)
{
Sign = "-";
}
Var strNum = num + "";
Var arr1 = strNum. split (".");
Var hasPoint = false; // whether the fractional part exists
Var piontPart = ""; // fractional part
Var intPart = strNum; // integer part
If (arr1.length> = 2)
{
HasPoint = true;
PiontPart = arr1 [1];
IntPart = arr1 [0];
}

Var res = ''; // Save the part with a comma
Var intPartlength = intPart. length; // integer part length
Var maxcount = Math. ceil (intPartlength/3); // you need to add several commas to the integer part.
For (var I = 1; I <= maxcount; I ++) // Add a comma for each three digits
{
Var startIndex = intPartlength-I * 3; // start position
If (startIndex <0) // when the start position is smaller than 0, the value is 0.
{
StartIndex = 0;
}
Var endIndex = intPartlength-I * 3 + 3; // end position
Var part = intPart. substring (startIndex, endIndex) + ",";
Res = part + res;
}
Res = res. substr (0, res. length-1); // remove the last comma
If (hasPoint)
{
Return "¥" + sign + res + "." + piontPart;
}
Else
{
Return "¥" + sign + res;
}

}

Related Article

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.