在括号中设置负数的格式,但不能使用$符号? [英] Format Negative numbers in parenthesis BUT NOT with $ symbol?

查看:184
本文介绍了在括号中设置负数的格式,但不能使用$符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上看到过,用带有 $ 符号的圆括号来格式化负双值。

I have seen all over the internet to format a NEGATIVE double value with a parenthesis WITH a $ symbol ie. currency type.

我正在寻找.NET格式的字符串,用于格式化

I am looking for a .NET format string, to format

12345.67 = 12,345.67

-12345.67 = (12,345.67)


推荐答案

条件格式化上的MSDN来解救!

MSDN on conditional formatting to the rescue!

您可以一次指定格式字符串的三个不同部分,并用分号分隔。如果指定两个格式字符串部分,则第一个用于正数和零值,第二个用于负数;如果使用三部分,则第一部分用于正值,第二部分用于负值,第三部分用于零值。

You can specify up to three different sections of your format string at once, separating them with semicolons. If you specify two format string sections, the first is used for positive and zero values while the second is used for negative values; if you use three sections, the first is used for positive values, the second for negative values, and the third for zero values.

此C#代码的输出:

        string fmt1 = "#,##0.00";
        string fmt2 = "#,##0.00;(#,##0.00)";
        double posAmount = 12345.67;
        double negAmount = -12345.67;
        Console.WriteLine("posAmount.ToString(fmt1) returns " + posAmount.ToString(fmt1));
        Console.WriteLine("negAmount.ToString(fmt1) returns " + negAmount.ToString(fmt1));
        Console.WriteLine("posAmount.ToString(fmt2) returns " + posAmount.ToString(fmt2));
        Console.WriteLine("negAmount.ToString(fmt2) returns " + negAmount.ToString(fmt2));

是:

posAmount.ToString(fmt1) returns 12,345.67
negAmount.ToString(fmt1) returns -12,345.67
posAmount.ToString(fmt2) returns 12,345.67
negAmount.ToString(fmt2) returns (12,345.67)

这篇关于在括号中设置负数的格式,但不能使用$符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆