将带小数的数字转换为文本

时间:2017-09-16 19:42:36

标签: c# visual-studio

我正在尝试将数字转换为文本,此代码在其中一个变量上给出了错误。

就在第三个变量的末尾,它给了我一个“预期的”错误。

代码中发生了什么?

public static string NumberToWords(double doubleNumber)
    {
        var beforeFloatingPoint = (int)Math.Floor(doubleNumber);
        var beforeFloatingPointWord = "{NumberToWords(beforeFloatingPoint)} Rupees";
        var afterFloatingPointWord = "{SmallNumberToWord((int) ((doubleNumber - beforeFloatingPoint) * 100),"")} cents";
        return "{beforeFloatingPointWord} and {afterFloatingPointWord}";
    }

错误正好在下面一行的末尾。

var afterFloatingPointWord = "{SmallNumberToWord((int) ((doubleNumber - beforeFloatingPoint) * 100),"")} cents";

完整代码在下面,现在当我运行代码时,我得到一个错误,说输入字符串格式不正确(在按钮单击事件中)。它没有小数。但是有了十进制,它给了我这个错误。我在下面发布完整的代码。

如果有人可以帮我解决这个问题。

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CheckPrintingSystem
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static string NumberToWords(double doubleNumber)
        {
            var beforeFloatingPoint = (int)Math.Floor(doubleNumber);
            var beforeFloatingPointWord = string.Format("NumberToWords(beforeFloatingPoint)} Rupees");
            var afterFloatingPointWord = string.Format(@"{SmallNumberToWord((int) ((doubleNumber - beforeFloatingPoint) * 100),"")} cents");
            return "{beforeFloatingPointWord} and {afterFloatingPointWord}";
        }

        private static string NumberToWords(int number)
        {


            if (number == 0) return "zero";

            if (number < 0) return "minus" + NumberToWords(Math.Abs(number));

            string words = "";

            if((number / 1000000) > 0)
            {
                words += NumberToWords(number / 1000000) + "million ";
                number %= 1000000;
            }

            if((number / 1000) > 0)
            {
                words += NumberToWords(number / 1000) + "thousand ";
                number %= 1000;
            }

            if ((number / 100) > 0)
            {
                words += NumberToWords(number / 100) + "hundred ";
                number %= 100;
            }

            words = SmallNumberToWord(number,words);
            return words;

        }

        private static string SmallNumberToWord(int number, string words)

        {
            if (number <= 0) return words;
            if (words != "") words += "and ";

                var unitsMap = new[] { "zero ", "one ", "two ", "three ", "four ", "five ", "six ", "seven ", "eight ", "nine ", "ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen "};
                var tensMap = new[] { "zero ", "ten ", "twenty ", "thirty ", "fourty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety " };
                if (number < 20) words += unitsMap[number];
                else
                {
                    words += tensMap[number / 10];

                    if ((number % 10) > 0) words += " " + unitsMap[number % 10];
                }
                return words;

        }



        private void button1_Click(object sender, EventArgs e)
        {
            textBox2.Text = NumberToWords(Convert.ToInt32(textBox1.Text));
        }
    }
}

1 个答案:

答案 0 :(得分:-2)

要纠正错误,您必须使用单引号作为空字符串。

所以代码必须是:

var afterFloatingPointWord = "{SmallNumberToWord((int) ((doubleNumber
- beforeFloatingPoint) * 100),'')} cents";

这是因为在第二个双引号中,字符串应该被关闭。

要使其余代码正常工作,您将不得不更改代码。

当您在:

中更改NumberToWords方法时,它将起作用
public static string NumberToWords(double doubleNumber)
{
    var beforeFloatingPoint = (int)Math.Floor(doubleNumber);
    var beforeFloatingPointWord = NumberToWords(beforeFloatingPoint) + " Rupees";
    var afterFloatingPointWord = SmallNumberToWord((int)((doubleNumber - beforeFloatingPoint) * 100), string.Empty) + " cents";
    return beforeFloatingPointWord + " and " + afterFloatingPointWord;
}