是否可以在代码后面实现脚本

发布于 2024-11-29 08:52:06 字数 842 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小草泠泠 2024-12-06 08:52:06

货币有标准格式:

double n = 12345.67;
Console.WriteLine(n.ToString("C2", CultureInfo.GetCultureInfo("en-US")));

Ouptut:

$12,345.67

然而,负货币值的标准是将其显示在括号内,因此如果您想要负号,则必须修改区域性:

CultureInfo info = new CultureInfo("en-US");
info.NumberFormat.CurrencyNegativePattern = 1;
double n = -12345.67;
Console.WriteLine(n.ToString("C2", info));

Ouptut:

-$12,345.67

There is a standard formatting for currency:

double n = 12345.67;
Console.WriteLine(n.ToString("C2", CultureInfo.GetCultureInfo("en-US")));

Ouptut:

$12,345.67

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:

CultureInfo info = new CultureInfo("en-US");
info.NumberFormat.CurrencyNegativePattern = 1;
double n = -12345.67;
Console.WriteLine(n.ToString("C2", info));

Ouptut:

-$12,345.67
墨小沫ゞ 2024-12-06 08:52:06
  1. 如果 123.12 + 123.12 = 246.23 那么您的客户可能会不高兴
  2. num 实际上是什么类型?看起来它只是一个数字,但您将其转换为字符串,删除 $, 符号,只是为了稍后添加它们?
  3. 您可以使用decimal.ToString(string format)方法,这样的方法应该会给您正确的结果:
static string Format(decimal x)
{
    return x.ToString("$#,0.00", System.Globalization.CultureInfo.InvariantCulture);
}

//Usage:
string currencyFormatedNumber = Format(-41023.43M); //returns -$41,023.43

您可以使用这样的方法:

static string Format(decimal x)
{
       return x.ToString("C", System.Globalization.CultureInfo.GetCultureInfo("en-us"));
}

它用于格式化货币,但它会格式化负数格式应为 ($100.00) 表示 -100.00 美元,这与您的代码不同。

  1. If 123.12 + 123.12 = 246.23 then your customers might not be to happy
  2. What type is num actually? It looks like it's just a number, but you're converting it to string, removing $ and , signs, just to add them later?
  3. You can use decimal.ToString(string format) method, something like this should give you proper results:
static string Format(decimal x)
{
    return x.ToString("$#,0.00", System.Globalization.CultureInfo.InvariantCulture);
}

//Usage:
string currencyFormatedNumber = Format(-41023.43M); //returns -$41,023.43

You could use something like this:

static string Format(decimal x)
{
       return x.ToString("C", System.Globalization.CultureInfo.GetCultureInfo("en-us"));
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文