我的应用程序当前将负数显示为 -1。用户更改了需求(只是为了更改!),现在我们必须将数字显示为 (1)。我可以为整个应用程序启用此功能(例如更改 web.config 甚至应用程序的 CultureInfo )吗?由于我们有很多包含数字验证器的页面,这样做是否有任何副作用?

谢谢 !

有帮助吗?

解决方案

对于货币来说,这很容易:

String.Format("{0:C}", value)

这将使用系统的文化信息。

对于数据绑定的正常数字,请使用 Mark Glorie的样本

MSDN文章

其他提示

我使用字符串格式。更改应用程序的配置以满足UI要求是非常严厉的。 SteveX写了一篇关于字符串格式的精彩博客文章。它还与标记(aspx)兼容,而不仅仅与代码相关。

从他的帖子:

String.Format(”{0:$#,##0.00;($#,##0.00);Zero}”, value);

    This will output “$1,240.00″ if passed 1243.50. It will output the 
    same format but in parentheses if the number is negative, and will
    output the string “Zero” if the number is zero.

这不是你想要的,但它很接近。

检查一下.. http://msdn.microsoft.com/en-us/library/91fwbcsb.aspx

将指定样式中数字的字符串表示形式转换为其Decimal等效项。

你总是可以编写自己的自定义ToString()方法作为扩展方法,但正如你所提到的,使用CultureInfo可能更好。看看这里:

http://msdn.microsoft.com /en-us/library/system.globalization.numberformatinfo.numbernegativepattern.aspx

我有以下页面标记为进行字符串格式化: http: //idunno.org/archive/2004/14/01/122.aspx

大约一半时间,它给出了答案:

String.Format("{0:£#,##0.00;(£#,##0.00);Nothing}", value);

要回答您的其他问题,我不会修改app.config以使其全局化,原因在于其他答案中给出的原因。

String.Format(”{0:f;(f);0”, -1);

这有效。

DataFormatString="{0:c0}"
  • 括号内为负数
  • 千位分隔符 - 逗号
  • 前面有$符号

您是否在Gridview / Datagrids中显示数据?如果是这样,则可以对每个绑定列应用格式,例如:

<asp:BoundField DataFormatString="{##;(##)}"/>

这仅适用于整数......

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top