简体   繁体   中英

Format to numbers entered using javascript

The case goes like this I have 4 textbox the dynamic is the following textbox1 + textbox2 + textbox 3 = textbox4. When entering the numbers in the textbox, I would like to give it the following format:

NOW: 123456789 WANTED: 123,456,789.00

 function agregar_numero() { var TextBox1 = parseInt(document.getElementById("txtremubruta").value); var TextBox2 = parseInt(document.getElementById("txtotrosingresos").value); var TextBox3 = parseInt(document.getElementById("txtremubrutamensual").value); var result = TextBox1 + TextBox2 + TextBox3; document.getElementById("TxtInfoPatriTotal").value = result; } 
 <form runat="server"> <asp:TextBox ID="txtremubruta" runat="server" CssClass="TextBoxBorder" Width="70px" onkeyup="agregar_numero()"></asp:TextBox> <asp:TextBox ID="txtotrosingresos" runat="server" CssClass="TextBoxBorder" Width="70px" onkeyup="agregar_numero()"></asp:TextBox> <asp:TextBox ID="txtremubrutamensual" runat="server" CssClass="TextBoxBorder" Width="70px" onkeyup="agregar_numero()"></asp:TextBox> <br> Total <br> <asp:TextBox ID="TxtInfoPatriTotal" runat="server" CssClass="TextBoxBorder" Width="70px"></asp:TextBox> </form> 

You can simply use toLocaleString method here's the code:

var num = 123456789;
num.toLocaleString(undefined, {minimumFractionDigits: 2}) // "123,456,789.00"

Hope it helps.

This may Helps:

function addComma(txt) {
txt.value = txt.value.replace(",", "").replace(/(\d+)(\d{3})/, "$1,$2");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM