如何将整数转换为其口头表示? [英] How can I convert an integer into its verbal representation?

查看:22
本文介绍了如何将整数转换为其口头表示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有库或类/函数可用于将整数转换为其口头表示?

Is there a library or a class/function that I can use to convert an integer to it's verbal representation?

示例输入:

4,567,788`

示例输出:

四百万、五十六万七千、七百八十八

Four million, Five hundred sixty-seven thousand, seven hundred eighty-eight

推荐答案

如果您使用以下代码:将数字转换为单词 C#并且您需要十进制数,这是如何做到的:

if you use the code found in: converting numbers in to words C# and you need it for decimal numbers, here is how to do it:

public string DecimalToWords(decimal number)
{
    if (number == 0)
        return "zero";

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

    string words = "";

    int intPortion = (int)number;
    decimal fraction = (number - intPortion)*100;
    int decPortion = (int)fraction;

    words = NumericToWords(intPortion);
    if (decPortion > 0)
    {
        words += " and ";
        words += NumericToWords(decPortion);
    }
    return words;
}

这篇关于如何将整数转换为其口头表示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆