将整数转换为书面数字
是否有一种有效的方法将整数转换为书面数字,例如:
string Written = IntegerToWritten(21);
将返回“二十一”。
有没有什么方法可以做到这一点而不涉及大量的查找表?
Is there an efficient method of converting an integer into the written numbers, for example:
string Written = IntegerToWritten(21);
would return "Twenty One".
Is there any way of doing this that doesn't involve a massive look-up table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
以下 C# 控制台应用程序代码将接受最多 2 位小数的货币值并以英语打印。 这不仅将整数转换为其等值的英文,而且还转换为以美元和美分表示的货币价值。
The following C# console app code will accepts a monetary value in numbers up to 2 decimals and prints it in English. This not only converts integer to its English equivalent but as a monetary value in dollars and cents.
我使用这个代码。它是 VB 代码,但您可以轻松地将其转换为 C#。 它可以工作
这是c#中的代码
I use this code.It is VB code but you can easily translate it to C#. It works
Here is the code in c#
为什么需要大量的查找表?
why massive lookup table?
从 1 到 19999 工作正常,我完成后很快就会更新
this works fine from 1 to 19999 will update soon after i complete it
接受的答案似乎并不完美。 它不处理像二十一这样的数字中的破折号,它不会在像“一百零一”这样的数字中放入单词“and”,而且,它是递归的。
这是我的答案。 它智能地添加“and”一词,并适当地用连字符连接数字。 如果需要任何修改,请告诉我。
下面是如何调用它(显然你会想把它放在一个类中的某个地方):
这是代码:
The accepted answer doesn't seem to work perfectly. It doesn't handle dashes in numbers like twenty-one, it doesn't put the word "and" in for numbers like "one hundred and one", and, well, it is recursive.
Here is my shot at the answer. It adds the "and" word intelligently, and hyphenates numbers appropriately. Let me know if any modifications are needed.
Here is how to call it (obviously you will want to put this in a class somewhere):
Here is the code:
这是一个 C# 控制台应用程序这将返回整数和小数。
Here is a C# Console Application that will return whole numbers as well as decimals.
仅用于类的土耳其语表示 HumanFriendlyInteger (↑) (Türkçe, sayı yazı karşılığı):
Just for Turkish representation of the class HumanFriendlyInteger (↑) (Türkçe, sayı yazı karşılığı):
Nick Masao 对同一问题的孟加拉数字答案的扩展。 数字的初始输入是 Unicode 字符串。 干杯!!
An extension of Nick Masao's answer for Bengali Numeric of same problem. Inital input of number is in Unicode string. Cheers!!
我使用这个名为 Humanizer 的方便库。
https://github.com/Humanizr/Humanizer
它支持多种文化,不仅可以将数字转换为单词,还可以将数字转换为单词。还可以日期,而且使用起来非常简单。
我的使用方法如下:
瞧!
I use this handy library called Humanizer.
https://github.com/Humanizr/Humanizer
It supports several cultures and converts not only numbers to words but also date and it's very simple to use.
Here's how I use it:
And voilá!
这应该工作得相当好:(
编辑以修复百万、十亿等的错误)
This should work reasonably well:
(Edited to fix a bug w/ million, billion, etc.)
只需获取该字符串并使用
就像
它只会写入完整的整数值
Just get that string and convert with the
like as
It will write only full integer value