Asp.net + js implements the amount formatting, and asp. netjs implements the amount

Source: Internet
Author: User

Asp.net + js implements the amount formatting, and asp. netjs implements the amount

You must format the input amount based on your needs. Each three digits are separated by commas (,) and two decimal places are retained.

Considering the user experience, the amount is formatted with JS. The front-end code is as follows:

Copy codeThe Code is as follows:
<Asp: TextBox ID = "txtAmount" runat = "server" onkeypress = "check ()" onkeyup = "run (this)"> </asp: TextBox>

The JS Code is as follows:

Copy codeThe Code is as follows:
// ====== Check whether the input is a number
Function check (){
If (! (Window. event. keyCode> = 48 & window. event. keyCode <= 57) | window. event. keyCode = 46 | window. event. keyCode = 45 )){
Window. event. keyCode = 0
}
}
// ======= Format the text box amount
Function run (obj ){
Var objvalue = obj. value. replace (/[,]/g ,""),
Objlength = objvalue. length,
Dtmp = objvalue. indexOf ("."),
Neg = objvalue. indexOf ("-");
Var inttmp = 0,
Floattmp =-1;
If (dtmp! =-1 ){
Inttmp = dtmp = 0? "0": new String (objvalue). substring (0, dtmp );
Floattmp = new String (objvalue). substring (dtmp + 1, objlength + 1 );
Floattmp = floattmp. replace (/[^ 0-9]/g ,"");
}
Else {
Inttmp = objvalue;
}
If (neg = 0 ){
Inttmp = inttmp. replace (/[-]/g ,"");
}
Inttmp = inttmp. replace (/[^ 0-9]/g ,"");
Var tmp = "", str = "0000 ";
For (; inttmp. length> 3 ;){
Var temp = new String (inttmp/1000 );
If (temp. indexOf (".") =-1 ){
Tmp = ", 000" + tmp;
Inttmp = temp;
}
Else {
Var le = new String (temp). split (".") [1]. length;
Tmp = "," + new String (temp). split (".") [1] + str. substring (0, 3-le) + tmp;
Inttmp = new String (temp). split (".") [0];
}
}
Inttmp = inttmp + tmp;
Obj. value = neg = 0? "-" + Inttmp + runing (floattmp): inttmp + runing (floattmp );
}
// ======= Sort the fractional part
Function runing (val ){
If (val! = "-1" & val! = ""){
Var valvalue = 0 + "." + val;
If (val. length> = 2 ){
Valvalue = parseFloat (valvalue). toFixed (2 );
}
Var temp = "." + valvalue. split (".") [1];
Return temp;
}
Else if (val! = "0" & val = ""){
Return ".";
}
Else {
Return "";
}
}

At the same time, because the amount can be entered into a negative number, add the "neg = objvalue. indexOf ("-")" judgment.

I often encounter this kind of problem about formatting the amount. I think this is okay and I will keep it for later convenience!

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.