Asp.net+js implementation amount Formatting _javascript tips

Source: Internet
Author: User

Depending on user requirements, format the amount you enter, that is, each three-digit number is separated by commas and retains two decimal places.

Taking into account the user experience, with the use of JS to format the amount, the foreground code is as follows:

Copy Code code as follows:

<asp:textbox id= "Txtamount" runat= "Server" onkeypress= "Check ()" onkeyup= "Run (This)" ></asp:TextBox>

The JS code is as follows:

Copy Code code as follows:

= = = = = Check whether the input is a number
function Check () {
if (!) ( (Window.event.keyCode >= && window.event.keyCode <= 57) | | Window.event.keyCode = 46 | | Window.event.keyCode = = 45)) {
Window.event.keyCode = 0
}
}
= = = = Amount of formatted text box
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 = "," + 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);
}
= = = To organize a small number of parts
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 negative, so add "neg = Objvalue.indexof ("-")" judgment.

About the amount of formatting problems, often encountered this kind of thing, think this can also be kept, for later access convenience!

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.