转换字符串到浮点数在C#字符串、浮点数

2023-09-04 22:47:49 作者:月牙弯弯

我将字符串转换成这样的41.00027357629127,我使用;

I am converting a string like this "41.00027357629127" and I am using;

Convert.ToSingle("41.00027357629127");

float.Parse("41.00027357629127");

这些方法返回4.10002732E + 15

these methods returns 4.10002732E+15

当我转换为浮动我想41.00027357629127这个字符串应该是一样的...

when I convert to float I want "41.00027357629127" this string should be same...

推荐答案

您线程的区域设置为在其中小数标记,而不是。

Your thread's locale is set to one in which the decimal mark is "," instead of ".".

请尝试使用这样的:

float.Parse("41.00027357629127", CultureInfo.InvariantCulture.NumberFormat);

请注意,但是,一个浮动不能认为许多precision数字。你将不得不使用双或小数这样做。

Note, however, that a float cannot hold that many digits of precision. You would have to use double or Decimal to do so.