简体   繁体   English

如何将小于1的字符串(“ 0.25500000”)转换为十进制?

[英]How to Convert string (“0.25500000”) less than 1 to Decimal?

I have a string "0.30405" and I need to convert it to a decimal. 我有一个字符串"0.30405" ,我需要将其转换为小数。 However it's throwing an error. 但是,它抛出一个错误。

What is the solution for this without blowing my head off 有什么解决方案,而不必惊blowing

Convert.ToDecimal("0.25500000") //throws exception

如果该行引发异常,则可能是因为您的区域性设置不允许逗号作为小数点分隔符。

尝试

Convert.ToDecimal("0.25500000", CultureInfo.InvariantCulture);

Try using decimal.TryParse() with a culture info specified. 尝试使用带有指定区域性信息的decimal.TryParse()

decimal number;
decimal.TryParse("0.25500000", NumberStyles.Number, 
                  CultureInfo.InvariantCulture, out number);

As someone pointed out in comments, in a production code you would probably want to find out if a conversion is successful by 正如某人在注释中指出的那样,在生产代码中,您可能希望通过以下方式确定转换是否成功

if(decimal.TryParse(...))
{
    // success
}

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

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