在Convert.ToDouble中忽略小数点 [英] Decimal Point ignored in Convert.ToDouble
问题描述
在C#Visual Studio中,读取后不将小数点加倍,并使用
In C# visual studio not taking decimal points in double after read and converted into double using
Convert.ToDouble(Console.ReadLine());
命令...例如,如果键入12.25
,它将值保存为1225
.我可以帮忙吗?
这是我正在使用的代码.
command... For example if 12.25
is typed it saves the value as 1225
. Can I get any help?
This is the Code i'm using.
double number;
Console.WriteLine("Enter a number with a two decimal places!");
number = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(number):
Console.ReadLine();
推荐答案
您当前的代码应能按预期工作,否则,可能是文化问题导致您的双字符串采用不同格式的结果(即12,25
而不是12.25
).
Your current code should be working as expected and if it isn't, it's likely the result of cultural issues that expect your double string to be in a different format (i.e. 12,25
instead of 12.25
).
解析时应用当前区域性
如果您希望处理句点或逗号,则可以用相同的方式执行替换:
If you wanted to expect to handle either periods or commas, you could potentially perform a replacement the in the same manner :
// Get the number separator for this culture and replace any others with it
var separator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
// Replace any periods or commas with the current culture separator and parse
var input = Double.Parse(Regex.Replace(Console.ReadLine(),"[.,]",separator));
您可以在此处查看有效的示例.
这篇关于在Convert.ToDouble中忽略小数点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!