C#格式为货币,没有尾随零十进制数,考虑为负数,不同文化

问题描述 投票:1回答:2

如何在不跟踪零十进制数的情况下将数字格式化为货币?基本上它应该表现为"C" format specifier但没有尾随零。以下是测试用例。

value   | en-US  | fr-FR
1       | $1     | 1 €
1.0     | $1     | 1 €
1.1     | $1.1   | 1,1 €
1.10    | $1.1   | 1,1 €
-1      | ($1)   | -1 €
-1.0    | ($1)   | -1 €
-1.1    | ($1.1) | -1,1 €
1000    | $1,000 | 1 000 €
1000.0  | $1,000 | 1 000 €

有没有办法通过利用“C”格式说明符来实现这种行为?

旁注:我继续从this related wpf question,但专注于格式化部分和更详尽的测试用例。

c# string-formatting cultureinfo currency-formatting
2个回答
0
投票

我想我能用以下方式回答我的问题:

public static class DecimalExtensions
{
    /// <summary>
    ///     Converts a numeric value to its equivalent currency string representation using the specified culture-specific format information.
    /// </summary>
    /// <param name="value">The value to be converted.</param>
    /// <param name="provider">An object that supplies culture-specific formatting information.</param>
    /// <returns>The currency string representation of the value as specified by <paramref name="provider" />.</returns>
    public static string ToCurrency(this decimal value, IFormatProvider provider) =>
        /// Use "1" (or "-1" if value is negative)
        /// as a placeholder for the actual value.
        (value < 0 ? -1 : 1)

        /// Format as a currency using the "C" format specifier.
        .ToString("C0", provider)

        /// Convert the absolute value to its string representation
        /// then replace the placeholder "1".
        /// We used absolute value since the negative sign
        /// is already converted to its string representation
        /// using the "C" format specifier.
        .Replace("1", Math.Abs(value).ToString("#,0.############################", provider));
}

Tests

public class ToCurrencyTests
{
    private string ToCurrency(decimal value, string cultureName) =>
        value.ToCurrency(CultureInfo.GetCultureInfo(cultureName));

    [Theory]
    [MemberData(nameof(enUS))]
    public void ToCurrency_enUS(decimal value, string expected) =>
    Assert.Equal(expected, ToCurrency(value, "en-US"));

    [Theory]
    [MemberData(nameof(frFR))]
    public void ToCurrency_frFR(decimal value, string expected) =>
        Assert.Equal(expected, ToCurrency(value, "fr-FR"));

    public static TheoryData<decimal, string> enUS =>
        new TheoryData<decimal, string>
        {
            { 1m, "$1" },
            { 1.0m, "$1" },
            { 1.1m, "$1.1" },
            { 1.10m, "$1.1" },
            { -1m, "($1)" },
            { -1.0m, "($1)" },
            { -1.1m, "($1.1)" },
            { 1000m, "$1,000" },
            { 1000.0m, "$1,000" },
            { 123456789.123456789m, "$123,456,789.123456789" },
            { .0000000000000000000000000001m, "$0.0000000000000000000000000001" }
        };

    /// <remarks>
    ///     Note that the group separator used here is a non-breaking space ' ' (i.e. &nbsp; or char 160)
    /// </remarks>
    public static TheoryData<decimal, string> frFR =>
        new TheoryData<decimal, string>
        {
            { 1m, "1 €" },
            { 1.0m, "1 €" },
            { 1.1m, "1,1 €" },
            { 1.10m, "1,1 €" },
            { -1m, "-1 €" },
            { -1.0m, "-1 €" },
            { -1.1m, "-1,1 €" },
            { 1000m, "1 000 €" },
            { 1000.0m, "1 000 €" },
            { 123456789.123456789m, "123 456 789,123456789 €" },
            { .0000000000000000000000000001m, "0,0000000000000000000000000001 €" }
        };
}

编辑:复制不间断的空间http://www.unicode-symbol.com/u/00A0.html


-2
投票

以下格式字符串将删除所有尾随零(如果它仅包含零位数,则删除整个小数部分)。

value.ToString( “#0 #####。”);

要处理负数,请将其与conditional formatting结合使用

value.ToString( “#0 #####;(#,0 #####)。”);

© www.soinside.com 2019 - 2024. All rights reserved.