如何转换字符串指数浮动?字符串、指数

2023-09-06 14:24:54 作者:越努力越lucky

我有以下字符串:3.39112632978e + 001,我需要转换为浮动。 WolframAlpha的说,这个值的结果是33.9112632978这显然我应该得到某种方式,我无法弄清楚如何。

I have the following string: "3.39112632978e+001" which I need to convert to float. WolframAlpha says that the result of this value is 33.9112632978 which evidently I should get somehow and I couldn't figure out how.

Single.Parse("3.39112632978e+001") gives 3.39112624E+12

Double.Parse("3.39112632978e+001") gives 3391126329780.0

float.Parse("3.39112632978e+001") gives 3.39112624E+12

我应该怎么办?

What should I do?

推荐答案

正在经历一个本土化的问题,其中正在跨preTED为千位分隔符而不是作为一个小数点分隔符。你在,说,欧洲?

You are experiencing a localization issue wherein the . is being interpreted as a thousands separator instead of as a decimal separator. Are you in, say, Europe?

试试这个:

float f = Single.Parse("3.39112632978e+001", CultureInfo.InvariantCulture);
Console.WriteLine(f);

输出:

33.91126

请注意,如果我们替换 然后我们看到,您所遇到的行为:

Note that if we replace the . by a , then we see the behavior that you are experiencing:

float g = Single.Parse("3,39112632978e+001", CultureInfo.InvariantCulture);
Console.WriteLine(g);

输出:

3.391126E+12

这支持了我的信念,你正在经历一个本土化的问题。

This supports my belief that you are experiencing a localization issue.