繁体   English   中英

不会将字符串转换为十进制c#(输入字符串的格式不正确。)

[英]Won't convert string to decimal c# (Input string was not in a correct format.)

Visual Studio不会将我的字符串转换为十进制

错误:输入字符串的格式不正确。

码:

string test = "123.95";
decimal test1 = decimal.parse(test); // string being an int "123" doesnt cause this

还有Convert.toDecimal(测试); 做同样的事情。

编辑:谢谢你的答案,我在网上看小数如何工作,每个人都在使用'。' 并不是 ','。 对不起我和这篇文章是多么令人难以置信的愚蠢。 再次感谢answeres :)

你现在的文化可能不会使用. 作为小数点分隔符。 在解析字符串时尝试指定不变文化

using System.Globalization;

...

string test = "123.95";
decimal test1 = decimal.Parse(test, CultureInfo.InvariantCulture); 

或者,您可以指定使用所需特定格式的区域性:

string test = "123.95";
var culture = new CultureInfo("en-US");
decimal test1 = decimal.Parse(test, culture); 

使用此代码 -

var test1 = "123.95";
decimal result;
decimal.TryParse(test1, out result);

它对我有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM