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!