2017-09-16 57 views
-3

我試圖將數字轉換爲文本,並且此代碼給了我一個變量的錯誤。將小數轉換爲文本

只是在第三個變量的末尾,它給了我一個「預期」的錯誤。

代碼中發生了什麼?

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)); 
     } 
    } 
} 
+0

要在正常的字符串插入一個雙引號字符使用反斜槓::

當您更改NumberToWords方法,它將正常工作。'\「'(不是另一雙引號)文字字符串(' @「...」')是不同的 – Richard

+2

你想使用字符串插值嗎?然後使用$「...」語法,並注意你如何用雙引號將雙引號括起來 - 你必須避開內部的引號 – orhtej2

+0

Guys我使用了string.Format並且錯誤消失了,但是當我運行代碼的時候出現了一個新的錯誤,我附上了上面的完整代碼,請幫助我理清它。@ orhtej2不能使用它給我的$ syntax意外的字符錯誤 –

回答

-2

要糾正錯誤,您必須爲空字符串使用單引號。

因此,代碼必須是:

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

這是因爲在第二個雙引號括起來的,預計將關閉。

要使代碼的其餘部分工作,您將不得不更改一些代碼。

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; 
} 
+0

我可以不使用單引號,因爲如果使用單引號,它會給第二和第三個變量提供兩個警告,說明這些值從未使用過。所以我用string.Format和錯誤消失了。但是當我運行代碼時出現新錯誤。沒關係,當我只有數字,但有小數時,我得到一個錯誤。我附上了上面的完整代碼。請幫我把它整理出來。 –

+0

我用新的NumberToWords實現改變了答案。這個double必須轉換成一個Int來讓事情起作用。 –

+0

非常感謝Koen Meijer,但是我在按鈕單擊事件中遇到錯誤。 private void button1_Click(object sender,EventArgs e) textBox2.Text = NumberToWords(Convert.ToInt32(textBox1.Text)); } –