转换成金钱/十进制字符串

编程入门 行业动态 更新时间:2024-10-19 00:28:56
本文介绍了转换成金钱/十进制字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用的是.NET。有一些code的地方,可以修改

I'm using . Is there some code somewhere that can change

$ 11,456.50 - > 11456和50/100美元

$11,456.50 -> eleven thousand four hundred fifty six and 50/100 Dollars

我可以做我自己,但不希望重写的东西已经存在。

I can do it myself, but don't want to rewrite something that's already there.

推荐答案

下面是一个未经雕琢的第一枪。完全没有错误检查和code不处理小数部分。而这几乎是未经检验的。随着的BigInteger 该方法可办理值高达 10 ^ 66 - 1 或约 219 位中加入更多的条目 M2 。

Here is an unpolished first shot. No error checking at all and the code does not handle the fractional part. And it is almost untested. With BigInteger the method can handle values up to 10^66 - 1 or about 219 bits easily extended by adding more entries to M2.

public static String NumberToText(Decimal n) { if (n < 0) { return "minus " + NumberToText(-n); } else if (n == 0) { return M1[n]; } else { var scale = 0; var parts = new List<String>(); while (n != 0) { if (n % 1000 != 0) { parts.Add(String.Format("{0} {1}", NumberToTextSmaller1000(n % 1000), M2[scale])); } n = Math.Floor(n / 1000); scale++; } parts.Reverse(); return String.Join(", ", parts.ToArray()); } } private static String NumberToTextSmaller1000(Decimal n) { var pattern = (n < 100) ? "{2}" : (n % 100 == 0) ? "{0} {1}" : "{0} {1} {2}"; return String.Format(pattern, M1[Math.Floor(n / 100)], M1[100], NumberToTextSmaller100(n % 100)); } private static String NumberToTextSmaller100(Decimal n) { return (0 <= n) && (n < 20)) || (n % 10 == 0) ? M1[n] : String.Format("{0}-{1}", M1[n - n % 10], M1[n % 10]; } private static readonly IDictionary<Decimal, String> M1 = new Dictionary<Decimal, String> { { 0, "zero" }, { 1, "one" }, { 2, "two" }, { 3, "three" }, { 4, "four" }, { 5, "five" }, { 6, "six" }, { 7, "seven" }, { 8, "eight" }, { 9, "nine" }, { 10, "ten" }, { 11, "eleven" }, { 12, "twelve" }, { 13, "thirteen" }, { 14, "fourteen" }, { 15, "fifteen" }, { 16, "sixteen" }, { 17, "seventeen" }, { 18, "eighteen" }, { 19, "nineteen" }, { 20, "twenty" }, { 30, "thirty" }, { 40, "forty" }, { 50, "fifty" }, { 60, "sixty" }, { 70, "seventy" }, { 80, "eighty" }, { 90, "ninety" }, { 100, "hundred" } }; // The leading spaces are important. private static readonly IDictionary<Decimal, String> M2 = new Dictionary<Decimal, String> { { 0, String.Empty }, { 1, " thousand" }, { 2, " million" }, { 3, " billion" }, { 4, " trillion" }, { 5, " quadrillion" }, { 6, " quintillion" }, { 7, " sextillion" }, { 8, " septillion" }, { 9, " octillion" }, { 10, " nonillion" }, { 11, " decillion" }, { 12, " undecillion" }, { 13, " duodecillion" }, { 14, " tredecillion" }, { 15, " quattuordecillion" }, { 16, " quindecillion" }, { 17, " sexdecillion" }, { 18, " septendecillion" }, { 19, " octodecillion" }, { 20, " novemdecillion" }, { 21, " vigintillion" } };

号召 Decimal.MaxValue 该方法返回下面的内容。

Called on Decimal.MaxValue the method returns the following.

79千的九次方,一百二十八两septillion,162 sextillion,514百万的三次方,264万亿,3370000亿五百九十个-3个十亿五百4300万,95万,335

seventy-nine octillion, two hundred twenty-eight septillion, one hundred sixty-two sextillion, five hundred fourteen quintillion, two hundred sixty-four quadrillion, three hundred thirty-seven trillion, five hundred ninety-three billion, five hundred forty-three million, nine hundred fifty thousand, three hundred thirty-five

更多推荐

转换成金钱/十进制字符串

本文发布于:2023-10-24 20:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524870.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换成   字符串   金钱   十进制

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!