货币到字符串

时间:2012-11-01 08:19:13

标签: c# .net currency

通常我会使用string.Format()来获取格式化的字符串。

但是我要求所有金额都应该打印成文本而不是数字。

即。类似的东西:

Format(3000, "us"); // => Resulting text: "three thousand dollars"

是否有可以处理的.NET库(俄语是强制性的)?

5 个答案:

答案 0 :(得分:2)

  

是否有可以处理的.NET库(俄语是强制性的)?

简短回答:否。

你必须自己编写,我建议你看一下以下内容: converting numbers in to words C#How can I convert an integer into its verbal representation?正如其中之一所说,请查看project Euler problem number 17并将其用于更广泛的Google搜索。

除此之外,您还有货币名称问题,'us'应该表示货币或语言,还是两者都有?加元是否与美元不同?

例如,这是内置的:

// Gives USD
var ISOCode = System.Globalization.RegionInfo.RegionInfo("US").ISOCurrencySymbol
// Gives $
var symbol = System.Globalization.RegionInfo.RegionInfo("US").CurrencySymbol
// Gives USD Dollar (i believe :))
var nameUSDENG = System.Globalization.RegionInfo.RegionInfo("US").CurrencyEnglishName
// Gives Svensk Krona
var nameSEKSWE = System.Globalization.RegionInfo.RegionInfo("SE").CurrencyNativeName
// Gives Swedish Krona
var nameSEKENG = System.Globalization.RegionInfo.RegionInfo("SE").CurrencyEnglishName

我建议你从那里开始,看看路径在哪里。

答案 1 :(得分:1)

我已经浏览了一下但是找不到合适的版本来转换为英语货币,所以我将几个结合到这个解决方案中:

public static class NumberExtensions
{
    public static String toCurrency(this double number, String major, String minor, String endStr, int minorLength)
    {
        return ((decimal)number).toCurrency(major, minor, endStr, minorLength);
    }

    public static String toCurrency(this decimal number, String major, String minor, String endStr, int minorLength)
    {

        if (String.IsNullOrWhiteSpace(major)) throw new Exception("Must specify major currency");
        if (String.IsNullOrWhiteSpace(minor)) throw new Exception("Must specify minor currency");

        String numb = number.ToString();
        String val = "", wholeNo = numb, points = "", andStr = major, pointStr = "";
        int decimalPlace = numb.IndexOf(".");
        if (decimalPlace > 0)
        {
            wholeNo = numb.Substring(0, decimalPlace);
            points = numb.Substring(decimalPlace + 1);
            if (points.Length > minorLength) throw new Exception("Incorrect format");
            if (Convert.ToInt32(points) > 0)
            {
                andStr = major + " and";
                endStr = minor + " " + endStr;
                pointStr = Int32.Parse(points.Length == 1 ? points + "0" : points).toWords();
            }
        }
        if (String.IsNullOrWhiteSpace(pointStr))
        {
            if (endStr == minor + " " || endStr == null)
            {
                val = String.Format("{0} {1}", Int32.Parse(wholeNo).toWords().Trim(), andStr.Trim());
            }
            else
            {
                val = String.Format("{0} {1} {2}", Int32.Parse(wholeNo).toWords().Trim(), andStr.Trim(), endStr.Trim());
            }
        }
        else
        {
            val = String.Format("{0} {1} {2} {3}", Int32.Parse(wholeNo).toWords().Trim(), andStr.Trim(), pointStr.Trim(), endStr.Trim());
        }
        return val;
    }

    public static string toWords(this int number)
    {
        if (number == 0)
            return "Zero";

        if (number < 0)
            return "minus " + Math.Abs(number).toWords();

        string words = "";

        if ((number / 1000000) > 0)
        {
            words += (number / 1000000).toWords().TrimEnd() + " Million ";
            number %= 1000000;
        }

        if ((number / 1000) > 0)
        {
            words += (number / 1000).toWords().TrimEnd() + " Thousand ";
            number %= 1000;
        }

        if ((number / 100) > 0)
        {
            words += (number / 100).toWords().TrimEnd() + " Hundred ";
            number %= 100;
        }

        if (number > 0)
        {
            if (words != "")
                words += "and ";

            var unitsMap = new[] { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
            var tensMap = new[] { "Zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };

            if (number < 20)
                words += unitsMap[number];
            else
            {
                words += tensMap[number / 10];
                if ((number % 10) > 0)
                    words += "-" + unitsMap[number % 10];
            }
        }

        return words;
    }
}

这是单元测试:

[TestFixture]
public class NumbericExtensionMethodsTests
    : TestBase
{
    [Test]
    [TestCase(0.0, null, null, null, 2, ExpectedException = typeof(Exception))]
    [TestCase(0.0, "", null, null, 2, ExpectedException = typeof(Exception))]
    [TestCase(0.0, null, "", null, 2, ExpectedException = typeof(Exception))]
    [TestCase(0.0, "", "", null, 2, ExpectedException = typeof(Exception))]
    [TestCase(0.001, "Rand", "Cent", null, 2, ExpectedException = typeof(Exception))]
    [TestCase(0.001, "Rand", "Cent", null, 3, Result = "Zero Rand and One Cent")]
    [TestCase(0, "Rand", "Cent", null, 2, Result = "Zero Rand")]
    [TestCase(0.0, "Rand", "Cent", null, 2, Result = "Zero Rand")]
    [TestCase(0.01, "Rand", "Cent", null, 2, Result = "Zero Rand and One Cent")]
    [TestCase(0.1, "Rand", "Cent", null, 2, Result = "Zero Rand and Ten Cent")]
    [TestCase(0.10, "Rand", "Cent", null, 2, Result = "Zero Rand and Ten Cent")]
    [TestCase(0.10, "Rand", "Cent", "only", 2, Result = "Zero Rand and Ten Cent only")]
    [TestCase(1.10, "Rand", "Cent", "only", 2, Result = "One Rand and Ten Cent only")]
    [TestCase(1.0, "Rand", "Cent", "only", 2, Result = "One Rand only")]
    [TestCase(123312.32, "Rand", "Cent", null, 2, Result = "One Hundred and Twenty-Three Thousand Three Hundred and Twelve Rand and Thirty-Two Cent")]
    public string NumberToCurrency(decimal number, string major, string minor, string endStr, int minorLength)
    {
        return number.toCurrency(major, minor, endStr, minorLength);
    }

    [Test]
    [TestCase(0, Result = "Zero")]
    [TestCase(1, Result = "One")]
    [TestCase(2, Result = "Two")]
    [TestCase(3, Result = "Three")]
    [TestCase(4, Result = "Four")]
    [TestCase(5, Result = "Five")]
    [TestCase(6, Result = "Six")]
    [TestCase(7, Result = "Seven")]
    [TestCase(8, Result = "Eight")]
    [TestCase(9, Result = "Nine")]

    [TestCase(10, Result = "Ten")]
    [TestCase(11, Result = "Eleven")]
    [TestCase(12, Result = "Twelve")]
    [TestCase(13, Result = "Thirteen")]
    [TestCase(14, Result = "Fourteen")]
    [TestCase(15, Result = "Fifteen")]
    [TestCase(16, Result = "Sixteen")]
    [TestCase(17, Result = "Seventeen")]
    [TestCase(18, Result = "Eighteen")]
    [TestCase(19, Result = "Nineteen")]
    [TestCase(20, Result = "Twenty")]
    [TestCase(30, Result = "Thirty")]
    [TestCase(40, Result = "Forty")]
    [TestCase(50, Result = "Fifty")]
    [TestCase(60, Result = "Sixty")]
    [TestCase(70, Result = "Seventy")]
    [TestCase(80, Result = "Eighty")]
    [TestCase(90, Result = "Ninety")]
    [TestCase(99, Result = "Ninety-Nine")]

    [TestCase(100, Result = "One Hundred")]
    [TestCase(101, Result = "One Hundred and One")]
    [TestCase(111, Result = "One Hundred and Eleven")]
    [TestCase(121, Result = "One Hundred and Twenty-One")]

    [TestCase(1000, Result = "One Thousand")]
    [TestCase(1001, Result = "One Thousand and One")]
    [TestCase(1011, Result = "One Thousand and Eleven")]
    [TestCase(1021, Result = "One Thousand and Twenty-One")]
    [TestCase(1100, Result = "One Thousand One Hundred")]
    [TestCase(1101, Result = "One Thousand One Hundred and One")]
    [TestCase(1111, Result = "One Thousand One Hundred and Eleven")]
    [TestCase(1121, Result = "One Thousand One Hundred and Twenty-One")]

    [TestCase(12000, Result = "Twelve Thousand")]
    [TestCase(12001, Result = "Twelve Thousand and One")]
    [TestCase(12011, Result = "Twelve Thousand and Eleven")]
    [TestCase(12021, Result = "Twelve Thousand and Twenty-One")]
    [TestCase(12100, Result = "Twelve Thousand One Hundred")]
    [TestCase(12101, Result = "Twelve Thousand One Hundred and One")]
    [TestCase(12111, Result = "Twelve Thousand One Hundred and Eleven")]
    [TestCase(12121, Result = "Twelve Thousand One Hundred and Twenty-One")]
    [TestCase(60000, Result = "Sixty Thousand")]
    [TestCase(60001, Result = "Sixty Thousand and One")]
    [TestCase(60011, Result = "Sixty Thousand and Eleven")]
    [TestCase(60021, Result = "Sixty Thousand and Twenty-One")]
    [TestCase(60100, Result = "Sixty Thousand One Hundred")]
    [TestCase(60101, Result = "Sixty Thousand One Hundred and One")]
    [TestCase(60111, Result = "Sixty Thousand One Hundred and Eleven")]
    [TestCase(60121, Result = "Sixty Thousand One Hundred and Twenty-One")]
    [TestCase(61000, Result = "Sixty-One Thousand")]
    [TestCase(61001, Result = "Sixty-One Thousand and One")]
    [TestCase(61011, Result = "Sixty-One Thousand and Eleven")]
    [TestCase(61021, Result = "Sixty-One Thousand and Twenty-One")]
    [TestCase(61100, Result = "Sixty-One Thousand One Hundred")]
    [TestCase(61101, Result = "Sixty-One Thousand One Hundred and One")]
    [TestCase(61111, Result = "Sixty-One Thousand One Hundred and Eleven")]
    [TestCase(61121, Result = "Sixty-One Thousand One Hundred and Twenty-One")]

    [TestCase(100000, Result = "One Hundred Thousand")]
    [TestCase(100001, Result = "One Hundred Thousand and One")]
    [TestCase(100011, Result = "One Hundred Thousand and Eleven")]
    [TestCase(100021, Result = "One Hundred Thousand and Twenty-One")]
    [TestCase(100100, Result = "One Hundred Thousand One Hundred")]
    [TestCase(100101, Result = "One Hundred Thousand One Hundred and One")]
    [TestCase(100111, Result = "One Hundred Thousand One Hundred and Eleven")]
    [TestCase(100121, Result = "One Hundred Thousand One Hundred and Twenty-One")]
    [TestCase(101000, Result = "One Hundred and One Thousand")]
    [TestCase(101001, Result = "One Hundred and One Thousand and One")]
    [TestCase(101011, Result = "One Hundred and One Thousand and Eleven")]
    [TestCase(101021, Result = "One Hundred and One Thousand and Twenty-One")]
    [TestCase(101100, Result = "One Hundred and One Thousand One Hundred")]
    [TestCase(101101, Result = "One Hundred and One Thousand One Hundred and One")]
    [TestCase(101111, Result = "One Hundred and One Thousand One Hundred and Eleven")]
    [TestCase(101121, Result = "One Hundred and One Thousand One Hundred and Twenty-One")]
    [TestCase(112000, Result = "One Hundred and Twelve Thousand")]
    [TestCase(112001, Result = "One Hundred and Twelve Thousand and One")]
    [TestCase(112011, Result = "One Hundred and Twelve Thousand and Eleven")]
    [TestCase(112021, Result = "One Hundred and Twelve Thousand and Twenty-One")]
    [TestCase(112100, Result = "One Hundred and Twelve Thousand One Hundred")]
    [TestCase(112101, Result = "One Hundred and Twelve Thousand One Hundred and One")]
    [TestCase(112111, Result = "One Hundred and Twelve Thousand One Hundred and Eleven")]
    [TestCase(112121, Result = "One Hundred and Twelve Thousand One Hundred and Twenty-One")]
    [TestCase(160000, Result = "One Hundred and Sixty Thousand")]
    [TestCase(160001, Result = "One Hundred and Sixty Thousand and One")]
    [TestCase(160011, Result = "One Hundred and Sixty Thousand and Eleven")]
    [TestCase(160021, Result = "One Hundred and Sixty Thousand and Twenty-One")]
    [TestCase(160100, Result = "One Hundred and Sixty Thousand One Hundred")]
    [TestCase(160101, Result = "One Hundred and Sixty Thousand One Hundred and One")]
    [TestCase(160111, Result = "One Hundred and Sixty Thousand One Hundred and Eleven")]
    [TestCase(160121, Result = "One Hundred and Sixty Thousand One Hundred and Twenty-One")]
    [TestCase(161000, Result = "One Hundred and Sixty-One Thousand")]
    [TestCase(161001, Result = "One Hundred and Sixty-One Thousand and One")]
    [TestCase(161011, Result = "One Hundred and Sixty-One Thousand and Eleven")]
    [TestCase(161021, Result = "One Hundred and Sixty-One Thousand and Twenty-One")]
    [TestCase(161100, Result = "One Hundred and Sixty-One Thousand One Hundred")]
    [TestCase(161101, Result = "One Hundred and Sixty-One Thousand One Hundred and One")]
    [TestCase(161111, Result = "One Hundred and Sixty-One Thousand One Hundred and Eleven")]
    [TestCase(161121, Result = "One Hundred and Sixty-One Thousand One Hundred and Twenty-One")]
    [TestCase(1000000, Result = "One Million")]
    [TestCase(Int32.MaxValue, Result = "Two Thousand One Hundred and Forty-Seven Million Four Hundred and Eighty-Three Thousand Six Hundred and Forty-Seven")]
    /*Assuming Millions will work if Thousands worked*/
    public string IntToWords(int number)
    {
        return number.toWords().Trim();
    }
}

完美享受这项工作:D

  

<强>积分

     

答案 2 :(得分:1)

我还没有测试过,但是这可能会解决你的问题吗?

Nuget Package

它说它也支持俄语。

来自包装说明:

  

Number To Text Converter。支持的语言英语俄语西班牙语土耳其语

用法:

var number = 123456.78
var moneyText = number.ToText("usd", "en");

var number = 123456.78;
var moneyText = number.ToText(Nut.Currency.USD, Nut.Language.English);

var number = 123456.78;
var options = new Nut.Options {
    MainUnitNotConvertedToText = true,
    SubUnitNotConvertedToText = true,
    MainUnitFirstCharUpper = true,
    SubUnitFirstCharUpper = true,
    CurrencyFirstCharUpper = true,
    SubUnitZeroNotDisplayed = true
}
var moneyText = number.ToText(Nut.Currency.USD, Nut.Language.English, options);

答案 3 :(得分:1)

有一个成熟的图书馆可以满足您的需求。它被称为人性化。

你可以在这里看到它的内容:NuGet Package of the Week: Humanizer makes .NET data types more human

open source project is on GitHub

正如您所看到的,它可以满足您的需求和更多的东西。它有很多本地化。

一些例子:

1.ToWords() => "one"
10.ToWords() => "ten"
11.ToWords() => "eleven"
122.ToWords() => "one hundred and twenty-two"
3501.ToWords() => "three thousand five hundred and one"
  

您还可以将第二个参数GrammaticalGender传递给ToWords,以指定该数字应输出的性别。

1.ToWords(GrammaticalGender.Masculine) => "один"
1.ToWords(GrammaticalGender.Feminine) => "одна"
1.ToWords(GrammaticalGender.Neuter) => "одно"

答案 4 :(得分:0)

对于俄语,您可以使用我的图书馆:https://github.com/nick-buhro/NumToWords
它也可以在nuget上使用:https://www.nuget.org/packages/NickBuhro.NumToWords

示例:

var unit = new UnitOfMeasure(Gender.Masculine, "доллар", "доллара", "долларов");
var text = RussianConverter.Format(3000, unit);
Console.WriteLine(text);
// Output: три тысячи долларов