格式化带有逗号和小数点的文本框 [英] Format text box with commas and decimal points
本文介绍了格式化带有逗号和小数点的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
全部,
我有两个文本框,当用户输入数字时,它们会自动添加逗号.这些文本框被添加到第三个文本框.它可以用逗号很好地工作.但是我希望所有文本框自动格式化为两个小数点,包括逗号(例如:1,345.00 + 3,456.00 = 4801.00).
我得到的这段代码可以很好地添加文本框并以逗号显示结果,但是它不会自动包含两个小数点.请帮我.提前谢谢.
Hie all,
I have two text boxes which automatically add commas when user enters number. These text boxes are added to third text box. It works fine with commas. But I want all text boxes to automatically format to two decimals including comma (Ex : 1,345.00 + 3,456.00 = 4801.00) .
I got this code which works fine to add text boxes and show result with comma but it does not automatically include two decimal points. Please help me. Thanks in advance.
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm8.aspx.cs" Inherits="Application.WebForm8" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script type="text/javascript" language="javascript">
function Comma(Num) { //function to add commas to textboxes
Num += '';
Num = Num.replace(',', ''); Num = Num.replace(',', ''); Num = Num.replace(',', '');
Num = Num.replace(',', ''); Num = Num.replace(',', ''); Num = Num.replace(',', '');
x = Num.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1))
x1 = x1.replace(rgx, '$1' + ',' + '$2');
return x1 + x2;
}
function sumCalc() // function to remove comma and then add to third textbox
{
var txt1 = document.getElementById('<%=tb1.ClientID %>').value.replace(/,/g, "");
var txt2 = document.getElementById('<%=tb2.ClientID %>').value.replace(/,/g, "");
var txt3 = document.getElementById('<%=tb3.ClientID %>').value = Comma(parseFloat(txt1) + parseFloat(txt2));
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:TextBox ID="tb1" runat="server" onkeyup = "javascript:this.value=Comma(this.value);" ></asp:TextBox>
<asp:TextBox ID="tb2" runat="server" onkeyup = "javascript:this.value=Comma(this.value);"></asp:TextBox>
<br />
<asp:Label ID="Label1" runat="server" Text="Result"></asp:Label>
<br />
<asp:TextBox ID="tb3" runat="server" onfocus="javascript:sumCalc();" ></asp:TextBox>
<br />
<br />
<br />
</asp:Content>
推荐答案
1' + ' '
1' + ',' + '
2'); 返回 x1 + x2; } 函数 sumCalc()// 函数删除逗号,然后添加到第三个文本框 { var txt1 = 文档 .getElementById(' <%= tb1.ClientID%>').value.replace(/,/g," "); var txt2 = 文档 .getElementById(' <%= tb2.ClientID%>').value.replace(/,/g," "); var txt3 = 文档 .getElementById(' <%= tb3.ClientID%>').value =逗号( parseFloat (txt1)+ parseFloat (txt2)); } < / 脚本 > < /asp:Content > < asp:Content ID =" ContentPlaceHolderID ContentPlaceHolder1 " runat 服务器" < asp:TextBox ID =" runat 服务器" onkeyup =" > /asp :TextBox > < asp:TextBox ID =" runat 服务器" onkeyup =" < > < br > < asp:Label ID =" runat 服务器" 文本 结果" < /asp:Label > < br > < asp:TextBox ID =" runat 服务器" onfocus javascript:sumCalc();" > < /asp:TextBox > < br > < br > < br > < /asp:Content >
2'); return x1 + x2; } function sumCalc() // function to remove comma and then add to third textbox { var txt1 = document.getElementById('<%=tb1.ClientID %>').value.replace(/,/g, ""); var txt2 = document.getElementById('<%=tb2.ClientID %>').value.replace(/,/g, ""); var txt3 = document.getElementById('<%=tb3.ClientID %>').value = Comma(parseFloat(txt1) + parseFloat(txt2)); } </script> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> <asp:TextBox ID="tb1" runat="server" onkeyup = "javascript:this.value=Comma(this.value);" ></asp:TextBox> <asp:TextBox ID="tb2" runat="server" onkeyup = "javascript:this.value=Comma(this.value);"></asp:TextBox> <br /> <asp:Label ID="Label1" runat="server" Text="Result"></asp:Label> <br /> <asp:TextBox ID="tb3" runat="server" onfocus="javascript:sumCalc();" ></asp:TextBox> <br /> <br /> <br /> </asp:Content>
function addCommas(clientID)
{
var nStr = document.getElementById(clientID.id).value;
nStr += '';
x = nStr.split('.');
if (!x[0] )
{
x[0] = "0";
}
x1 = x[0];
if (!x[1] )
{
x[1] = "00";
}
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '
这篇关于格式化带有逗号和小数点的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文