How can I convert an integer into its verbal representation?

后端 未结 14 2176
走了就别回头了
走了就别回头了 2020-11-22 15:23

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

Example input:

4,567,788`

14条回答
  •  逝去的感伤
    2020-11-22 16:11

    Solution that takes up less code.

    The most important part is only couple lines:

    static Func remainder = t => t > 0 ? " " + ToEN(t) : "";
    
    public static string ToEN(this long val, double d = 20, long th = 20)
    {
        switch ((long)d)
        {
            case 20:   return val >= d ? ToEN(val, 1e2)             : en[val];
            case 100:  return val >= d ? ToEN(val, 1e3, 100)        : en[val / 10 * 10] + remainder(val % 10);
            default:   return val >= d ? ToEN(val, d * 1e3,(long)d) : ToEN(val / th) + " " + en[th] + remainder(val % th);
        }
    }
    

    Full code is available here https://dotnetfiddle.net/wjr4hF

提交回复
热议问题