开发者

How can I convert between numbers and the words that represent them? [duplicate]

开发者 https://www.devze.com 2023-01-26 06:02 出处:网络
This question already has answers here: 开发者_StackOverflow中文版 Closed 12 years ago. Sorry if this is a silly question, but it is useful for my current lexical analysis project. Is there an
This question already has answers here: 开发者_StackOverflow中文版 Closed 12 years ago.

Sorry if this is a silly question, but it is useful for my current lexical analysis project. Is there any built-in and preferably culture-based (language-based) function in the .Net framework to convert back and forth from string with numbers and the words that represent them? Like "1" <-> "one", "2" <-> "two", "3" <-> "three" and like this?

I know this can be made using a dictionary or a list with all the valid correspondences, but that is too much work if there is already a built-in function that one can use.

Edit: People who voted to close may have not understood that I was not asking how to convert decimal representation to strings, but the reverse operation: from a string to it's decimal representation. The links provided do not address this problem.

Thank you very much.


No, this is not built into the .NET Framework. You will have to develop a custom algorithm to implement this functionality.

However, I recommend that instead of a dictionary or list (in which you will have to include every single number that you want to be able to display as a string), that you use something like the method described here, which is far more extensible.

And obviously, you should keep in mind that you need to be very careful with this kind of feature if you ever plan on internationalizing your application.


I don't think there is such a conversion (not to mention culture based). Take a look at a similar question for some implementations.


No, there is no built in function for that.

I remember that it was golfed a while back, you can check out some code to get a grasp of how it can be done: Code golf: Number to Words


This tool by BlackWasp should be worth taking a look.

0

精彩评论

暂无评论...
验证码 换一张
取 消