C# 打印拼写的数字(int)值

C# 打印拼写的数字(int)值,c#,C#,有没有现成的方法可以用C#来拼写int?例如,如果我有: int a = 53; 我想打印: "fifty three" 不是 如果没有,有没有人能举例说明如何做到这一点 谢谢 你必须自己编写代码。如果不得不猜测的话,我会说它不在框架中,因为它几乎不可能本地化(这不仅仅是数字的名称:词序、连字符规则等等) 这让我想起了往事,因为这是我大学第一门编程课程的作业,所以用英语写一篇应该不会太难。如果其他方法都失败了,请询问比尔·盖茨和他的团队。这应该让你开始 提供了将整数转换为单词的解决方案 下面

有没有现成的方法可以用C#来拼写int?例如,如果我有:

int a = 53;
我想打印:

"fifty three"
不是

如果没有,有没有人能举例说明如何做到这一点


谢谢

你必须自己编写代码。如果不得不猜测的话,我会说它不在框架中,因为它几乎不可能本地化(这不仅仅是数字的名称:词序、连字符规则等等)


这让我想起了往事,因为这是我大学第一门编程课程的作业,所以用英语写一篇应该不会太难。

如果其他方法都失败了,请询问比尔·盖茨和他的团队。这应该让你开始

提供了将整数转换为单词的解决方案

下面是将数字转换为数字的代码 语言

objnumber.changeNumericToWord(100)

使用该函数,您将获得数字 变成文字

资料来源: 作者:Hiran Repakula


他使用实例方法,但您可以使这些方法成为静态的,这里有一个额外的方法,只是为了好玩

public static class NumericSpelling
{
    private const long Quadrillion = Trillion * 1000;
    private const long Trillion = Billion * 1000;
    private const long Billion = Million * 1000;
    private const long Million = Thousand * 1000;
    private const long Thousand = Hundred * 10;
    private const long Hundred = 100;

    public static string ToVerbal(this int value) { return ToVerbal((long)value); }

    public static string ToVerbal(this long value)
    {
        if (value == 0) return "zero";

        if (value < 0)
        {
            return "negative " + ToVerbal(Math.Abs(value));
        }

        System.Text.StringBuilder builder = new StringBuilder();

        int unit = 0;

        if (value >= Quadrillion)
        {
            unit = (int)(value / Quadrillion);
            value -= unit * Quadrillion;

            builder.AppendFormat("{0}{1} quadrillion", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit));
        }

        if (value >= Trillion)
        {
            unit = (int)(value / Trillion);
            value -= unit * Trillion;

            builder.AppendFormat("{0}{1} trillion", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit));
        }

        if (value >= Billion)
        {
            unit = (int)(value / Billion);
            value -= unit * Billion;

            builder.AppendFormat("{0}{1} billion", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit));
        }

        if (value >= Million)
        {
            unit = (int)(value / Million);
            value -= unit * Million;

            builder.AppendFormat("{0}{1} million", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit));
        }

        if (value >= Thousand)
        {
            unit = (int)(value / Thousand);
            value -= unit * Thousand;

            builder.AppendFormat("{0}{1} thousand", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit));
        }

        if (value >= Hundred)
        {
            unit = (int)(value / Hundred);
            value -= unit * Hundred;

            builder.AppendFormat("{0}{1} hundred", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit));
        }

        if (builder.Length > 0 && value > 0) builder.AppendFormat(" and");

        if (value >= 90)
        {
            value -= 90;

            builder.AppendFormat("{0}ninety", builder.Length > 0 ? " " : string.Empty);
        }

        if (value >= 80)
        {
            value -= 80;

            builder.AppendFormat("{0}eighty", builder.Length > 0 ? " " : string.Empty);
        }

        if (value >= 70)
        {
            value -= 70;

            builder.AppendFormat("{0}seventy", builder.Length > 0 ? " " : string.Empty);
        }

        if (value >= 60)
        {
            value -= 60;

            builder.AppendFormat("{0}sixty", builder.Length > 0 ? " " : string.Empty);
        }

        if (value >= 50)
        {
            value -= 50;

            builder.AppendFormat("{0}fifty", builder.Length > 0 ? " " : string.Empty);
        }

        if (value >= 40)
        {
            value -= 40;

            builder.AppendFormat("{0}forty", builder.Length > 0 ? " " : string.Empty);
        }

        if (value >= 30)
        {
            value -= 30;

            builder.AppendFormat("{0}thirty", builder.Length > 0 ? " " : string.Empty);
        }

        if (value >= 20)
        {
            value -= 20;

            builder.AppendFormat("{0}twenty", builder.Length > 0 ? " " : string.Empty);
        }

        if (value == 19) builder.AppendFormat("{0}nineteen", builder.Length > 0 ? " " : string.Empty);
        if (value == 18) builder.AppendFormat("{0}eighteen", builder.Length > 0 ? " " : string.Empty);
        if (value == 17) builder.AppendFormat("{0}seventeen", builder.Length > 0 ? " " : string.Empty);
        if (value == 16) builder.AppendFormat("{0}sixteen", builder.Length > 0 ? " " : string.Empty);
        if (value == 15) builder.AppendFormat("{0}fifteen", builder.Length > 0 ? " " : string.Empty);
        if (value == 14) builder.AppendFormat("{0}fourteen", builder.Length > 0 ? " " : string.Empty);
        if (value == 13) builder.AppendFormat("{0}thirteen", builder.Length > 0 ? " " : string.Empty);
        if (value == 12) builder.AppendFormat("{0}twelve", builder.Length > 0 ? " " : string.Empty);
        if (value == 11) builder.AppendFormat("{0}eleven", builder.Length > 0 ? " " : string.Empty);
        if (value == 10) builder.AppendFormat("{0}ten", builder.Length > 0 ? " " : string.Empty);
        if (value == 9) builder.AppendFormat("{0}nine", builder.Length > 0 ? " " : string.Empty);
        if (value == 8) builder.AppendFormat("{0}eight", builder.Length > 0 ? " " : string.Empty);
        if (value == 7) builder.AppendFormat("{0}seven", builder.Length > 0 ? " " : string.Empty);
        if (value == 6) builder.AppendFormat("{0}six", builder.Length > 0 ? " " : string.Empty);
        if (value == 5) builder.AppendFormat("{0}five", builder.Length > 0 ? " " : string.Empty);
        if (value == 4) builder.AppendFormat("{0}four", builder.Length > 0 ? " " : string.Empty);
        if (value == 3) builder.AppendFormat("{0}three", builder.Length > 0 ? " " : string.Empty);
        if (value == 2) builder.AppendFormat("{0}two", builder.Length > 0 ? " " : string.Empty);
        if (value == 1) builder.AppendFormat("{0}one", builder.Length > 0 ? " " : string.Empty);

        return builder.ToString();
    }
}
输出:

ten thousand, four hundred and forty seven
ten trillion, five hundred and seventy six billion, seven hundred and forty nine million, three hundred and twenty three thousand, four hundred and seventy five
zero
negative one thousand and ninety five
one hundred
one hundred and two
ten thousand and four
one hundred thousand and twenty five

是的,有一种方法可以做到这一点。该项目有一个带有“拼写”选项的基于规则的数字格式化程序。它甚至支持完全本地化

唯一的障碍是它只能在C、C++和java中使用。该项目中有一项计划(针对.NET Framework和.NET标准1.6),但此功能和许多其他功能尚未移植。然而,一项贡献可以很好地解决这个问题

还有一个应该能够围绕ICU4C库自动生成一个C#wrapper库,但我还没有尝试过


可用于其他编程语言。

本地化将是一件痛苦的事情——我只是在想法语“98”四行(字面意思是“4218”)的麻烦本地化通常是一件痛苦的事情,但对于一些小的定制,如连字符,这确实应该是开箱即用的。您能告诉我如何通过采用OOP概念、更干净的代码和TDD来实现这一点吗。
NumberToEnglish objnumber = new NumberToEnglish();
public static class NumericSpelling
{
    private const long Quadrillion = Trillion * 1000;
    private const long Trillion = Billion * 1000;
    private const long Billion = Million * 1000;
    private const long Million = Thousand * 1000;
    private const long Thousand = Hundred * 10;
    private const long Hundred = 100;

    public static string ToVerbal(this int value) { return ToVerbal((long)value); }

    public static string ToVerbal(this long value)
    {
        if (value == 0) return "zero";

        if (value < 0)
        {
            return "negative " + ToVerbal(Math.Abs(value));
        }

        System.Text.StringBuilder builder = new StringBuilder();

        int unit = 0;

        if (value >= Quadrillion)
        {
            unit = (int)(value / Quadrillion);
            value -= unit * Quadrillion;

            builder.AppendFormat("{0}{1} quadrillion", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit));
        }

        if (value >= Trillion)
        {
            unit = (int)(value / Trillion);
            value -= unit * Trillion;

            builder.AppendFormat("{0}{1} trillion", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit));
        }

        if (value >= Billion)
        {
            unit = (int)(value / Billion);
            value -= unit * Billion;

            builder.AppendFormat("{0}{1} billion", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit));
        }

        if (value >= Million)
        {
            unit = (int)(value / Million);
            value -= unit * Million;

            builder.AppendFormat("{0}{1} million", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit));
        }

        if (value >= Thousand)
        {
            unit = (int)(value / Thousand);
            value -= unit * Thousand;

            builder.AppendFormat("{0}{1} thousand", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit));
        }

        if (value >= Hundred)
        {
            unit = (int)(value / Hundred);
            value -= unit * Hundred;

            builder.AppendFormat("{0}{1} hundred", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit));
        }

        if (builder.Length > 0 && value > 0) builder.AppendFormat(" and");

        if (value >= 90)
        {
            value -= 90;

            builder.AppendFormat("{0}ninety", builder.Length > 0 ? " " : string.Empty);
        }

        if (value >= 80)
        {
            value -= 80;

            builder.AppendFormat("{0}eighty", builder.Length > 0 ? " " : string.Empty);
        }

        if (value >= 70)
        {
            value -= 70;

            builder.AppendFormat("{0}seventy", builder.Length > 0 ? " " : string.Empty);
        }

        if (value >= 60)
        {
            value -= 60;

            builder.AppendFormat("{0}sixty", builder.Length > 0 ? " " : string.Empty);
        }

        if (value >= 50)
        {
            value -= 50;

            builder.AppendFormat("{0}fifty", builder.Length > 0 ? " " : string.Empty);
        }

        if (value >= 40)
        {
            value -= 40;

            builder.AppendFormat("{0}forty", builder.Length > 0 ? " " : string.Empty);
        }

        if (value >= 30)
        {
            value -= 30;

            builder.AppendFormat("{0}thirty", builder.Length > 0 ? " " : string.Empty);
        }

        if (value >= 20)
        {
            value -= 20;

            builder.AppendFormat("{0}twenty", builder.Length > 0 ? " " : string.Empty);
        }

        if (value == 19) builder.AppendFormat("{0}nineteen", builder.Length > 0 ? " " : string.Empty);
        if (value == 18) builder.AppendFormat("{0}eighteen", builder.Length > 0 ? " " : string.Empty);
        if (value == 17) builder.AppendFormat("{0}seventeen", builder.Length > 0 ? " " : string.Empty);
        if (value == 16) builder.AppendFormat("{0}sixteen", builder.Length > 0 ? " " : string.Empty);
        if (value == 15) builder.AppendFormat("{0}fifteen", builder.Length > 0 ? " " : string.Empty);
        if (value == 14) builder.AppendFormat("{0}fourteen", builder.Length > 0 ? " " : string.Empty);
        if (value == 13) builder.AppendFormat("{0}thirteen", builder.Length > 0 ? " " : string.Empty);
        if (value == 12) builder.AppendFormat("{0}twelve", builder.Length > 0 ? " " : string.Empty);
        if (value == 11) builder.AppendFormat("{0}eleven", builder.Length > 0 ? " " : string.Empty);
        if (value == 10) builder.AppendFormat("{0}ten", builder.Length > 0 ? " " : string.Empty);
        if (value == 9) builder.AppendFormat("{0}nine", builder.Length > 0 ? " " : string.Empty);
        if (value == 8) builder.AppendFormat("{0}eight", builder.Length > 0 ? " " : string.Empty);
        if (value == 7) builder.AppendFormat("{0}seven", builder.Length > 0 ? " " : string.Empty);
        if (value == 6) builder.AppendFormat("{0}six", builder.Length > 0 ? " " : string.Empty);
        if (value == 5) builder.AppendFormat("{0}five", builder.Length > 0 ? " " : string.Empty);
        if (value == 4) builder.AppendFormat("{0}four", builder.Length > 0 ? " " : string.Empty);
        if (value == 3) builder.AppendFormat("{0}three", builder.Length > 0 ? " " : string.Empty);
        if (value == 2) builder.AppendFormat("{0}two", builder.Length > 0 ? " " : string.Empty);
        if (value == 1) builder.AppendFormat("{0}one", builder.Length > 0 ? " " : string.Empty);

        return builder.ToString();
    }
}
int first = 10447;
long second = 10576749323475;
int third = 0;
int fourth = -1095;
int fifth = 100;
int sixth = 102;
int seventh = 10004;
int eight = 100025;

Console.WriteLine(first.ToVerbal());
Console.WriteLine(second.ToVerbal());
Console.WriteLine(third.ToVerbal());
Console.WriteLine(fourth.ToVerbal());
Console.WriteLine(fifth.ToVerbal());
Console.WriteLine(sixth.ToVerbal());
Console.WriteLine(seventh.ToVerbal());
Console.WriteLine(eight.ToVerbal());
ten thousand, four hundred and forty seven
ten trillion, five hundred and seventy six billion, seven hundred and forty nine million, three hundred and twenty three thousand, four hundred and seventy five
zero
negative one thousand and ninety five
one hundred
one hundred and two
ten thousand and four
one hundred thousand and twenty five