如何转换字符串指数浮动? [英] How to convert string with exponent to float?

查看:182
本文介绍了如何转换字符串指数浮动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下字符串: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.

这篇关于如何转换字符串指数浮动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆