Lut*_*ske 2 c# string floating-point converter
我必须将字符串转换为浮点数,只有普通的转换器不起作用.
fi.Resolution = float.Parse(nodeC.InnerText);
fi.Resolution = (float)nodeC.InnerText;
fi.Resolution = Single.Parse(nodeC.InnerText);
Run Code Online (Sandbox Code Playgroud)
而且更多这些方法不起作用.当nodeC.InnerText为0.01时,它返回1,但如果nodeC.InnerText为5.72958e-07,则返回0,0575958,0.0001也返回1,因此不是它的位移.
有谁知道为什么这个标准的c#转换不起作用?
所以我正在尝试编写自己的StringToFloat方法,但它失败了:P
public float StringToFloat(string input)
{
float output = 0;
char[] arr = input.ToCharArray();
for (int i = 0; i < input.Length - 1; i++)
{
if (arr[i].Equals("."))
output += 1;//change
else
output += Convert.ToInt32(arr[i]);
}
return output;
}
Run Code Online (Sandbox Code Playgroud)
Raw*_*ing 10
尝试 fi.Resolution = float.Parse(nodeC.InnerText, CultureInfo.InvariantCulture);
看起来您当前的文化期待,作为小数点分隔符而忽略任何.存在.
于是
0.01 => 001 => 1
5.72958e-07 => 572958e-07 => 0,0572958 (note it gave you a , not a .)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1027 次 |
| 最近记录: |