簡體   English   中英

C#格式為貨幣,沒有尾隨零的十進制數字,請考慮負數和不同的區域性

[英]C# format as currency with no trailing zero decimal numbers, consider negative, and different culture

如何在不尾隨零小數的情況下將數字格式化為貨幣? 基本上,它的行為應與“ C”格式說明符相同,但不能尾隨零。 以下是測試案例。

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”格式說明符來實現此行為?

旁注:我從這個有關wpf的問題繼續,但重點是格式化部分和更詳盡的測試用例。

我認為我可以通過以下方式回答我的問題:

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));
}

測驗

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

以下格式字符串刪除所有結尾的零(如果僅包含零數字,則刪除整個分數部分)。

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

要處理負數,請將其與條件格式結合使用

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM