我試圖將數字轉換爲文本,並且此代碼給了我一個變量的錯誤。將小數轉換爲文本
只是在第三個變量的末尾,它給了我一個「預期」的錯誤。
代碼中發生了什麼?
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));
}
}
}
要在正常的字符串插入一個雙引號字符使用反斜槓::
當您更改NumberToWords方法,它將正常工作。'\「'(不是另一雙引號)文字字符串(' @「...」')是不同的 – Richard
你想使用字符串插值嗎?然後使用$「...」語法,並注意你如何用雙引號將雙引號括起來 - 你必須避開內部的引號 – orhtej2
Guys我使用了string.Format並且錯誤消失了,但是當我運行代碼的時候出現了一個新的錯誤,我附上了上面的完整代碼,請幫助我理清它。@ orhtej2不能使用它給我的$ syntax意外的字符錯誤 –