String.Format("{0:C2}", -1234)(货币格式)将负数视为正数

发布于 2024-07-23 16:16:03 字数 121 浏览 8 评论 0 原文

我正在使用 String.Format("{0:C2}", -1234) 来格式化数字。

它总是将金额格式化为正数,而我希望它变成 $ - 1234

I am using String.Format("{0:C2}", -1234) to format numbers.

It always formats the amount to a positive number, while I want it to become $ - 1234

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

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

发布评论

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

评论(4

别忘他 2024-07-30 16:16:03

我说的正确吗?它将它放在括号中,即将其格式化为 ($1,234.00) ? 如果是这样,我相信这就是美国的预期行为。

不过,您可以创建自己的 NumberFormatInfo,但它的行为方式并非如此。 获取“大部分正确”的现有 NumberFormatInfo,调用 Clone() 制作可变副本,然后设置 CurrencyNegativePattern 适当(我认为您想要值 2)。

例如:

using System;
using System.Globalization;

class Test
{
    static void Main()
    {
        var usCulture = CultureInfo.CreateSpecificCulture("en-US");
        var clonedNumbers = (NumberFormatInfo) usCulture.NumberFormat.Clone();
        clonedNumbers.CurrencyNegativePattern = 2;
        string formatted = string.Format(clonedNumbers, "{0:C2}", -1234);
        Console.WriteLine(formatted);
    }
}

这将打印 $-1,234.00。 如果您确实想要 $-1234,则需要设置 CurrencyGroupSizes 属性为 new int[]{0} 并使用 "{0:C0}" 而不是 < code>"{0:C2}" 作为格式字符串。

编辑:这是一个您可以使用的辅助方法,它基本上做同样的事情:

private static readonly NumberFormatInfo CurrencyFormat = CreateCurrencyFormat();

private static NumberFormatInfo CreateCurrencyFormat()
{
    var usCulture = CultureInfo.CreateSpecificCulture("en-US");
    var clonedNumbers = (NumberFormatInfo) usCulture.NumberFormat.Clone();
    clonedNumbers.CurrencyNegativePattern = 2;
    return clonedNumbers;
}

public static string FormatCurrency(decimal value)
{
    return value.ToString("C2", CurrencyFormat);
}

Am I right in saying it's putting it in brackets, i.e. it's formatting it as ($1,234.00) ? If so, I believe that's the intended behaviour for the US.

However, you can create your own NumberFormatInfo which doesn't behave this way. Take an existing NumberFormatInfo which is "mostly right", call Clone() to make a mutable copy, and then set the CurrencyNegativePattern appropriately (I think you want value 2).

For example:

using System;
using System.Globalization;

class Test
{
    static void Main()
    {
        var usCulture = CultureInfo.CreateSpecificCulture("en-US");
        var clonedNumbers = (NumberFormatInfo) usCulture.NumberFormat.Clone();
        clonedNumbers.CurrencyNegativePattern = 2;
        string formatted = string.Format(clonedNumbers, "{0:C2}", -1234);
        Console.WriteLine(formatted);
    }
}

This prints $-1,234.00. If you actually want exactly $-1234, you'll need to set the CurrencyGroupSizes property to new int[]{0} and use "{0:C0}" instead of "{0:C2}" as the format string.

EDIT: Here's a helper method you can use which basically does the same thing:

private static readonly NumberFormatInfo CurrencyFormat = CreateCurrencyFormat();

private static NumberFormatInfo CreateCurrencyFormat()
{
    var usCulture = CultureInfo.CreateSpecificCulture("en-US");
    var clonedNumbers = (NumberFormatInfo) usCulture.NumberFormat.Clone();
    clonedNumbers.CurrencyNegativePattern = 2;
    return clonedNumbers;
}

public static string FormatCurrency(decimal value)
{
    return value.ToString("C2", CurrencyFormat);
}
内心荒芜 2024-07-30 16:16:03

另一个简单的选项是手动指定格式字符串。

String.Format("{0:$#,##0.00}", -1234)

或者,如果货币符号需要作为参数,您可以这样做

String.Format("{0:" + symbol + "#,##0.00}", -1234)

Another simple option is manually specify the format string.

String.Format("{0:$#,##0.00}", -1234)

Or, if the currency symbol needs to be a parameter, you could do this

String.Format("{0:" + symbol + "#,##0.00}", -1234)
等待圉鍢 2024-07-30 16:16:03

我想我会简单地使用:(

FormatCurrency(-1234.56, 2, UseParensForNegativeNumbers:=TriState.False)

在 Microsoft.VisualBasic.Strings 模块中)

或者用更短的话来说(这是我实际要使用的):

FormatCurrency(-1234.56, 2, 0, 0)

或者我将为自己创建一个自定义格式货币函数,该函数使用传递我的自定义参数的 VB 函数。

有关更多详细信息,请查看 FormatCurrency 函数 (Visual Basic) 在 msdn.

I think I will simply use:

FormatCurrency(-1234.56, 2, UseParensForNegativeNumbers:=TriState.False)

(in Microsoft.VisualBasic.Strings module)

Or in shorter words (this is what im actually going to use):

FormatCurrency(-1234.56, 2, 0, 0)

Or I will make myself a custom formatcurrency function that uses the VB function passing my custom params.

For further details take a look at the FormatCurrency Function (Visual Basic) in the msdn.

像你 2024-07-30 16:16:03
#region Format value to currency
    /// <summary>
    /// Method to format input in currency format
    ///  Input:  -12345.55
    ///  Output:$-12,345.55  
    /// </summary>
    /// <param name="Input"></param>
    /// <returns></returns>
    public static string FormatToCurrency(string Input)
    {
        try
        {
            decimal value = Convert.ToDecimal(Input);
            CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
            culture.NumberFormat.NumberNegativePattern = 2;
            return string.Format(culture, "{0:C}", value);
        }
        catch (Exception)
        {
            return string.Empty;
        }
    }
    #endregion
#region Format value to currency
    /// <summary>
    /// Method to format input in currency format
    ///  Input:  -12345.55
    ///  Output:$-12,345.55  
    /// </summary>
    /// <param name="Input"></param>
    /// <returns></returns>
    public static string FormatToCurrency(string Input)
    {
        try
        {
            decimal value = Convert.ToDecimal(Input);
            CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
            culture.NumberFormat.NumberNegativePattern = 2;
            return string.Format(culture, "{0:C}", value);
        }
        catch (Exception)
        {
            return string.Empty;
        }
    }
    #endregion
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文