整数转换单词的字符串 [英] Convert an Integer to a string of words

查看:122
本文介绍了整数转换单词的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
   C#转换整数成书面号

我需要一个整数值,并转换成它的英文单词相当于(即4 =>四大1879 =>一千元,879)在.NET(3.5要具体)

I need to take an integer value and convert it to its english word equivalent (i.e. 4 => "four", 1879 => "one thousand, eight hundred seventy-nine") in .NET (3.5 to be specific).

我不知道是否有什么内置在.NET框架作出这样的转换。好像这将是足够的有用的属于那里。我一直没能找到什么来完成这项工作。

I'm wondering if there is anything built into the .NET framework for making such a conversion. Seems like it would be useful enough to belong there. I haven't been able to find anything to do the job.

如果它不包含在任何地方的框架,没有任何人有任何想法比数字/地方具体查找更优雅?

If it isn't included in the framework anywhere, does anyone have any ideas more elegant than a digit/place specific lookup?

推荐答案

字符串s是输入数字

      const string input = "1023";

        string[] placement = { "thousand", "hundred", "ten", "" };
        string[] numbersToLetters = { "", "one", "two", "tre", "four", "five", "six", "seven", "eight", "nine" };

        for (int i = 0; i < input.Length; i++)
        {
            int  digits = input[i] != '0' ? (placement.Length - input.Length) + i : 3;
            int result = int.Parse(input[i].ToString());

            var type = placement[digits];
            var number = numbersToLetters[result];
            Console.WriteLine(number + type);
        }

这篇关于整数转换单词的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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