如何将带指数的字符串转换为浮点型?

发布于 2024-08-21 04:01:44 字数 320 浏览 4 评论 0 原文

我有以下字符串:“3.39112632978e+001”,我需要将其转换为浮点数。 WolframAlpha 说这个值的结果是 33.9112632978 显然我应该以某种方式得到,但我不知道如何得到。

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

我应该怎么办?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟花易冷人易散 2024-08-28 04:01:44

您遇到了本地化问题,其中 . 被解释为千位分隔符而不是小数分隔符。比如说,你在欧洲吗?

试试这个:

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

输出:

33.91126

请注意,如果我们将 . 替换为 ,,那么我们会看到您正在经历的行为:

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

输出:

3.391126E+12

这支持了我的信念,即您正在经历本地化问题。

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?

Try this:

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

Output:

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);

Output:

3.391126E+12

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

一袭白衣梦中忆 2024-08-28 04:01:44

我认为,该线程提示了您的问题: http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/e482cda0-6510-4d2c-b830-11e57e04f65d(以及System.Globalization. NumberStyles.Float 是这里的关键内容之一 - 它改变了 . 的解释方式)

I think, this thread gives hints to your question: http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/e482cda0-6510-4d2c-b830-11e57e04f65d (and the System.Globalization.NumberStyles.Float is one of the key things here - it changes how the . is interpreted)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文