如果我有一个输入字符串,输入有三个或更多的小数,我希望字符串由一个单独的if-loop
处理。
我为此创建了以下控制台程序,其中输入if-loop
,如果(第一个约束)有一个小数点,字符串中的.
和(第二个约束),如果小数位数是三或更多。
using System;
namespace Test
{
class Program
{
static void Main(string[] args)
{
string input = "0.065";
string output;
Console.WriteLine(input.ToString()); // (1)
Console.WriteLine(input.ToString().IndexOf(".")); // (2)
Console.WriteLine(input.ToString().Substring(input.ToString().IndexOf("."))); // (3)
if (Convert.ToInt32(input.IndexOf('.')) != -1 &&
Convert.ToInt32(input.Substring(input.IndexOf('.') + 1).Length) >= 3)
{
output = input.Substring(input.IndexOf('.') + 1);
Console.WriteLine(Convert.ToInt32(output));
}
Console.ReadLine();
}
}
}
这产生了预期的结果,即
(1)打印0.065
,
(2)打印1
,
(3)打印.065
(4)打印3
。
我的问题是,当我删除硬编码的字符串值,input = "0.065"
并替换它
input = Row["Price"].ToString();
其中Row["Price"].ToString();
是一个XML文件的值,它也有值0.065
我从控制台获得以下打印:
(1)产量0.065
(2)产量-1
(应该是1,因为存在.
)
(3)产量0.065
(应该是.065
)
(4)没有印刷,因为没有达到if constraints
。
有谁知道为什么会这样?我是C#的新手,但我认为既然input
在两种情况下都是string
类型,那么应该没有任何区别?特别是当我将ToString()
应用到我的新string input
?
当你做,
时,用那个字符串中的.
替换input = Row["Price"].ToString();
。
替换功能是input = Row["Price"].ToString().Replace(",", ".");