String.Format 相同代码不同视图

发布于 2024-10-30 19:51:46 字数 442 浏览 1 评论 0原文

我有这样的代码;

GridView1.FooterRow.Cells[11].Text = String.Format("{0:c}", sumKV)

在我的计算机中,这段代码给出了这样的结果;

在此处输入图像描述

但是当我将此代码上传到我的虚拟机时,它看起来像这样;

在此处输入图像描述

TL 表示土耳其里拉。但我不想显示货币。我只想要数字。

我也不想改变数字的格式。 (如257.579,02)

如何才能只删除这段代码中的TL

I have a code like this;

GridView1.FooterRow.Cells[11].Text = String.Format("{0:c}", sumKV)

In my computer this code gives a result like that;

enter image description here

But when I upload this code to my virtual machine it looks like this;

enter image description here

TL means Turkish Liras. But I don't want to show the currency. I just want numbers.

I also don't want to change the formating of numbers. (Like 257.579,02)

How can I only delete TL in this code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

酒几许 2024-11-06 19:51:46

我会用这个:

var cultureWithoutCurrencySymbol = 
    (CultureInfo)CultureInfo.CurrentCulture.Clone();
cultureWithoutCurrencySymbol.NumberFormat.CurrencySymbol = "";
GridView1.FooterRow.Cells[11].Text = 
            String.Format(cultureWithoutCurrencySymbol, "{0:c}", sumKV).Trim();

背景:
这仍将保留当前区域性的货币格式,只是删除货币符号。
您可以将这种特殊的文化保存在某个地方,这样您就不必每次需要格式化值时都创建它。

更新:

  • 现在它甚至可以编译...;-)
  • 添加了Trim(),因为格式化的数字后面仍然有一个空格。

I would use this:

var cultureWithoutCurrencySymbol = 
    (CultureInfo)CultureInfo.CurrentCulture.Clone();
cultureWithoutCurrencySymbol.NumberFormat.CurrencySymbol = "";
GridView1.FooterRow.Cells[11].Text = 
            String.Format(cultureWithoutCurrencySymbol, "{0:c}", sumKV).Trim();

Background:
This will still keep the currency formatting for the current culture, it just removes the currency symbol.
You can save this special culture somewhere, so you don't have to create it every time you need to format your values.

UPDATE:

  • Now it even compiles... ;-)
  • Added a Trim(), because there is still a space after the formated number.
巷子口的你 2024-11-06 19:51:46

另一种选择是完全关闭当前线程的货币符号:

private static NumberFormatInfo SetNoCurrencySymbol()
{
    CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
    NumberFormatInfo LocalFormat = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();

    NumberFormatInfo ret = culture.NumberFormat;

    LocalFormat.CurrencySymbol = "";
    culture.NumberFormat = LocalFormat;

    // Add the culture to the current thread
    Thread.CurrentThread.CurrentCulture = culture;

    return ret;
} 

这样您将更改更少的代码。之后您可以随时将其更改回来:

 NumberFormatInfo origNumberFormat  = SetNoCurrencySymbol();

 string x = String.Format("{0:c}", 55);

 CultureInfo.CurrentCulture.NumberFormat = origNumberFormat;

 string y = String.Format("{0:c}", 55);

Another option is to turn off the currency symbol entirely for the current thread:

private static NumberFormatInfo SetNoCurrencySymbol()
{
    CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
    NumberFormatInfo LocalFormat = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();

    NumberFormatInfo ret = culture.NumberFormat;

    LocalFormat.CurrencySymbol = "";
    culture.NumberFormat = LocalFormat;

    // Add the culture to the current thread
    Thread.CurrentThread.CurrentCulture = culture;

    return ret;
} 

That way you will change less code. You can always change it back afterwards:

 NumberFormatInfo origNumberFormat  = SetNoCurrencySymbol();

 string x = String.Format("{0:c}", 55);

 CultureInfo.CurrentCulture.NumberFormat = origNumberFormat;

 string y = String.Format("{0:c}", 55);
半城柳色半声笛 2024-11-06 19:51:46

由于您仅将 String.Format 与格式字符串一起使用,因此 sumKV 会根据应用程序中实际使用的 UI 区域性进行格式化。

GridView1.FooterRow.Cells[11].Text = String.Format("{0:c}", sumKV),

要摆脱货币符号,请在 String.Format 中使用 InvariantCulture,如下所示:

String.Format(CultureInfo.InvariantCulture, "{0:c}", sumKV);

Because you are using String.Format with a format string only, sumKV is formatted according to the UI Culture actually used in your application.

GridView1.FooterRow.Cells[11].Text = String.Format("{0:c}", sumKV),

To get rid with currency symbol, use InvariantCulture in String.Format this way :

String.Format(CultureInfo.InvariantCulture, "{0:c}", sumKV);
反目相谮 2024-11-06 19:51:46

如果您不想显示货币,则不要使用货币格式代码 - {0:c}。
也许尝试类似以下内容:

GridView1.FooterRow.Cells[11].Text = String.Format("{0:G}", sumKV);

查看这篇文章 - String.Format 双精度

If you don't want to show currency then don't use the currency formatting code - {0:c}.
Perhaps try something like the following:

GridView1.FooterRow.Cells[11].Text = String.Format("{0:G}", sumKV);

See this article - String.Format doubles

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