是否可以在代码后面实现脚本
我有一个 javascript 代码,可以将金额转换为美国格式,与我希望在后面的代码中使用的格式相同,任何人都可以帮助我..
<script type="text/javascript">
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}
</script>
或者如果有任何更简单的方法请告诉我。我的实际要求是将文本框中的两个给定金额相加,并在第三个文本框中显示金额。
例如,如果我通过上述脚本将第一个值设置为 123.12
,我将在文本框中显示为 $123.12
,第二个值与 $123.12
相同code> 输出应为 $246.23
谁能帮我吗
I have a javascript code which conver the amount in to US format the same i would like to have in code behind can any one help me..
<script type="text/javascript">
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '
Or if there is any easier method tell me. My actual requirement is to sum the two given amounts in the text boxes and to display the amount in 3rd text box.
For example if i have my 1st value as 123.12
by the above script i will get the display in my text box as $123.12
and second as same to $123.12
the output should be $246.23
can any one help me
+ num + '.' + cents);
}
</script>
Or if there is any easier method tell me. My actual requirement is to sum the two given amounts in the text boxes and to display the amount in 3rd text box.
For example if i have my 1st value as 123.12
by the above script i will get the display in my text box as $123.12
and second as same to $123.12
the output should be $246.23
can any one help me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
货币有标准格式:
Ouptut:
然而,负货币值的标准是将其显示在括号内,因此如果您想要负号,则必须修改区域性:
Ouptut:
There is a standard formatting for currency:
Ouptut:
The standard for negative currency values is however to display it within parentheses, so if you want a negative sign instead you have to modify the culture:
Ouptut:
123.12 + 123.12 = 246.23
那么您的客户可能会不高兴$
和,
符号,只是为了稍后添加它们?decimal.ToString(string format)
方法,这样的方法应该会给您正确的结果:您可以使用这样的方法:
它用于格式化货币,但它会格式化负数格式应为
($100.00)
表示-100.00 美元
,这与您的代码不同。123.12 + 123.12 = 246.23
then your customers might not be to happy$
and,
signs, just to add them later?decimal.ToString(string format)
method, something like this should give you proper results:You could use something like this:
which is used to format currency, but it will format negative numbers as they should be formatted
($100.00)
means-100.00 dollars
, which differs from your code.