在文本框中输入金额时,千分位自动给加上逗号,谢谢,回答必给分 如题~! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 decimal.ToString("###,###,###.###0");decimal是你数据类型,括号里的参数根据你的需要可以自己改改。 在TextChange或者onkeydown事件里边改(比如textbox叫a)a.text = (system.convert.todecimal(a.text)).Tostring("###,###,###.###0"); 简单的写了一个,看看符合要求不 <script type="text/javascript"> var flag=0; function c() { var obj=document.getElementById("TextBox1"); var v=obj.value; var l=v.length; if(l%4==0 && flag==0) { flag=1; obj.value=v.substring(0,l-3) + "," + v.substring(l-3,l); } else { flag=0; } } </script> <asp:TextBox ID="TextBox1" onpropertychange="c()" runat="server"></asp:TextBox> 找到一个答案:http://topic.csdn.net/t/20040907/13/3348056.html public string Commas(int nNum) { string str = nNum.ToString(); int nIndex = str.Length; while (nIndex > 3) { str = str.Insert(nIndex - 3, ","); nIndex -= 3; } return str; } 呵,我刚做了测试<script language=javascript>function FormatNum(number,pattern){代码内容请见http://topic.csdn.net/t/20040907/13/3348056.html}function showResult(){ var num=document.getElementById("Text1").value.replace(/,/g,""); document.getElementById("Text1").value=FormatNum(num,'#,###');}</script><input id="Text1" type="text" onfocusout="showResult()" />测试后结果正确 上面写的好像不对,试试这个 function c() { var obj=document.getElementById("TextBox1"); var v=obj.value; var leftValue; var temp=''; var len; v=v.replace(/,/g,""); document.getElementById("Text1").value=v; len=v.length; if(len>3 && flag==0) { flag=1; while(len>3) { leftValue=v.substring(0,len-3); alert(leftValue); temp="," + v.substring(len-3,len)+temp; v=leftValue; len = v.length; } obj.value=v+temp; temp=''; flag=0; } } 现在程序员的思路真让我无语...先format(1222222.34444,"##,##0")处理整数部分然后处理小数部分,不就是千分位加一个逗号吗... 客户端的代码: <script type="text/javascript"> <!-- function aa(v) { var len=String(parseInt(v)).length; while(len>3) { v=v.substr(0, len-3)+','+v.substr(len-3); len=len-3; } return v; } //--> </script><input type="text" onblur="this.value=aa(this.value)" /> 不防参考一下: decimal dec = 1234567890M; string newValue = string.Format("{0:N}", dec); 好像还是有问题,汗。。 function c() { var obj=document.getElementById("TextBox1"); var v=obj.value; var leftValue; var temp=''; var len; v=v.replace(/,/g,""); len=v.length; if(len>3 && flag==0) { flag=1; while(len>3) { leftValue=v.substring(0,len-3); temp="," + v.substring(len-3,len)+temp; v=leftValue; len = v.length; } obj.value=v+temp; temp=''; flag=0; } } 13楼代码正解。这种情况用 javascript 好一点。还用.net里边的东西就有点绕道了。弄了变天.net也是用script来实现。还不如直接用javascript解决。 在TextChange或者onkeydown事件里边改 (比如textbox叫a) a.text = (system.convert.todecimal(a.text)).Tostring("###,###,###.###0"); decimal.ToString("★★★,★★★,★★★.★★★0"); 格式化文本框输入每3位数字一个逗号http://www.aspxboy.com/private/showthread.asp?threadid=754 复杂的,但又通用的,其中有你要的代码://将数字格式化输出的javascript函数 function FormatNum(number,pattern) { //alert("sdasd"); var str = number.toString(); var strInt; var strFloat; var formatInt; var formatFloat; if(/\./g.test(pattern)) { formatInt = pattern.split('.')[0]; formatFloat = pattern.split('.')[1]; } else { formatInt = pattern; formatFloat = null; } if(/\./g.test(str)) { if(formatFloat!=null) { var tempFloat = Math.round(parseFloat('0.'+str.split('.')[1])*Math.pow(10,formatFloat.length))/Math.pow(10,formatFloat.length); strInt = (Math.floor(number)+Math.floor(tempFloat)).toString(); strFloat = /\./g.test(tempFloat.toString())?tempFloat.toString().split('.')[1]:'0'; } else { strInt = Math.round(number).toString(); strFloat = '0'; } } else { strInt = str; strFloat = '0'; } if(formatInt!=null) { var outputInt = ''; var zero = formatInt.match(/0*$/)[0].length; var comma = null; if(/,/g.test(formatInt)) { comma = formatInt.match(/,[^,]*/)[0].length-1; } var newReg = new RegExp('(\\d{'+comma+'})','g'); if(strInt.length<zero) { outputInt = new Array(zero+1).join('0')+strInt; outputInt = outputInt.substr(outputInt.length-zero,zero) } else { outputInt = strInt; } var outputInt = outputInt.substr(0,outputInt.length%comma)+outputInt.substring(outputInt.length%comma).replace(newReg,(comma!=null?',':'')+'$1') outputInt = outputInt.replace(/^,/,''); strInt = outputInt; } if(formatFloat!=null) { var outputFloat = ''; var zero = formatFloat.match(/^0*/)[0].length; if(strFloat.length<zero) { outputFloat = strFloat+new Array(zero+1).join('0'); //outputFloat = outputFloat.substring(0,formatFloat.length); var outputFloat1 = outputFloat.substring(0,zero); var outputFloat2 = outputFloat.substring(zero,formatFloat.length); outputFloat = outputFloat1+outputFloat2.replace(/0*$/,''); } else { outputFloat = strFloat.substring(0,formatFloat.length); } strFloat = outputFloat; } else { if(pattern!='' || (pattern=='' && strFloat=='0')) { strFloat = ''; } } return strInt+(strFloat==''?'':'.'+strFloat); } // 示例 // alert(formatNum(0,'')); // alert(formatNum(12432.21,'#,###')); // alert(formatNum(12432.21,'#,###.000#')); // alert(formatNum(12432,'#,###.00')); // alert(formatNum(12432.419,'#,###.0#')); /////////////////////////////////////////////////////// 一个关于vs2003问题 .net根据数据库时间字段到时间执行 一个禁止页面刷新的问题 在线等,为什么不能判断数据库里存在的数据.??附带问题代码 IIS问题 表格线可以变细些吗? 又开始重金相谢了。 网站怎么样远程读写文件服务器上的目录和文件? 你认为dlinq的出现,会不会把市面上的一些orm框架都灭掉? ImageButton如何实现点击关闭窗口? asp.net如何实现图片与文章排列? 关于VS2008WebForm中System.Web.UI.WebControls.ListViewItem
decimal是你数据类型,括号里的参数根据你的需要可以自己改改。
(比如textbox叫a)a.text = (system.convert.todecimal(a.text)).Tostring("###,###,###.###0");
var flag=0;
function c()
{
var obj=document.getElementById("TextBox1");
var v=obj.value;
var l=v.length;
if(l%4==0 && flag==0)
{
flag=1;
obj.value=v.substring(0,l-3) + "," + v.substring(l-3,l);
}
else
{
flag=0;
}
}
</script> <asp:TextBox ID="TextBox1" onpropertychange="c()" runat="server"></asp:TextBox>
http://topic.csdn.net/t/20040907/13/3348056.html
{
string str = nNum.ToString();
int nIndex = str.Length;
while (nIndex > 3)
{
str = str.Insert(nIndex - 3, ",");
nIndex -= 3;
}
return str;
}
<script language=javascript>
function FormatNum(number,pattern)
{
代码内容请见http://topic.csdn.net/t/20040907/13/3348056.html
}
function showResult()
{
var num=document.getElementById("Text1").value.replace(/,/g,"");
document.getElementById("Text1").value=FormatNum(num,'#,###');
}
</script><input id="Text1" type="text" onfocusout="showResult()" />
测试后结果正确
{
var obj=document.getElementById("TextBox1");
var v=obj.value;
var leftValue;
var temp='';
var len;
v=v.replace(/,/g,"");
document.getElementById("Text1").value=v;
len=v.length;
if(len>3 && flag==0)
{
flag=1;
while(len>3)
{
leftValue=v.substring(0,len-3);
alert(leftValue);
temp="," + v.substring(len-3,len)+temp;
v=leftValue;
len = v.length;
}
obj.value=v+temp;
temp='';
flag=0;
}
}
先format(1222222.34444,"##,##0")处理整数部分
然后处理小数部分,不就是千分位加一个逗号吗...
<script type="text/javascript">
<!--
function aa(v)
{
var len=String(parseInt(v)).length;
while(len>3)
{
v=v.substr(0, len-3)+','+v.substr(len-3);
len=len-3;
}
return v;
}
//-->
</script><input type="text" onblur="this.value=aa(this.value)" />
decimal dec = 1234567890M;
string newValue = string.Format("{0:N}", dec);
{
var obj=document.getElementById("TextBox1");
var v=obj.value;
var leftValue;
var temp='';
var len;
v=v.replace(/,/g,"");
len=v.length;
if(len>3 && flag==0)
{
flag=1;
while(len>3)
{
leftValue=v.substring(0,len-3);
temp="," + v.substring(len-3,len)+temp;
v=leftValue;
len = v.length;
}
obj.value=v+temp;
temp='';
flag=0;
}
}
这种情况用 javascript 好一点。还用.net里边的东西就有点绕道了。弄了变天.net也是用script来实现。还不如直接用javascript解决。
(比如textbox叫a) a.text = (system.convert.todecimal(a.text)).Tostring("###,###,###.###0");
http://www.aspxboy.com/private/showthread.asp?threadid=754
function FormatNum(number,pattern)
{
//alert("sdasd");
var str = number.toString();
var strInt;
var strFloat;
var formatInt;
var formatFloat;
if(/\./g.test(pattern))
{
formatInt = pattern.split('.')[0];
formatFloat = pattern.split('.')[1];
}
else
{
formatInt = pattern;
formatFloat = null;
}
if(/\./g.test(str))
{
if(formatFloat!=null)
{
var tempFloat = Math.round(parseFloat('0.'+str.split('.')[1])*Math.pow(10,formatFloat.length))/Math.pow(10,formatFloat.length);
strInt = (Math.floor(number)+Math.floor(tempFloat)).toString();
strFloat = /\./g.test(tempFloat.toString())?tempFloat.toString().split('.')[1]:'0';
}
else
{
strInt = Math.round(number).toString();
strFloat = '0';
}
}
else
{
strInt = str;
strFloat = '0';
}
if(formatInt!=null)
{
var outputInt = '';
var zero = formatInt.match(/0*$/)[0].length;
var comma = null;
if(/,/g.test(formatInt))
{
comma = formatInt.match(/,[^,]*/)[0].length-1;
}
var newReg = new RegExp('(\\d{'+comma+'})','g');
if(strInt.length<zero)
{
outputInt = new Array(zero+1).join('0')+strInt;
outputInt = outputInt.substr(outputInt.length-zero,zero)
}
else
{
outputInt = strInt;
}
var
outputInt = outputInt.substr(0,outputInt.length%comma)+outputInt.substring(outputInt.length%comma).replace(newReg,(comma!=null?',':'')+'$1')
outputInt = outputInt.replace(/^,/,'');
strInt = outputInt;
}
if(formatFloat!=null)
{
var outputFloat = '';
var zero = formatFloat.match(/^0*/)[0].length;
if(strFloat.length<zero)
{
outputFloat = strFloat+new Array(zero+1).join('0');
//outputFloat = outputFloat.substring(0,formatFloat.length);
var outputFloat1 = outputFloat.substring(0,zero);
var outputFloat2 = outputFloat.substring(zero,formatFloat.length);
outputFloat = outputFloat1+outputFloat2.replace(/0*$/,'');
}
else
{
outputFloat = strFloat.substring(0,formatFloat.length);
}
strFloat = outputFloat;
}
else
{
if(pattern!='' || (pattern=='' && strFloat=='0'))
{
strFloat = '';
}
}
return strInt+(strFloat==''?'':'.'+strFloat);
}
// 示例
// alert(formatNum(0,''));
// alert(formatNum(12432.21,'#,###'));
// alert(formatNum(12432.21,'#,###.000#'));
// alert(formatNum(12432,'#,###.00'));
// alert(formatNum(12432.419,'#,###.0#'));
///////////////////////////////////////////////////////