如何将字符串解析为 Double

发布于 2024-10-06 04:20:48 字数 230 浏览 0 评论 0原文

这是我的字符串,

  20.0e-6

我正在解析它,

String Ans=Double.Parse("20.0e-6")

现在我得到像 2E-05 这样的结果 但所需的输出应该是这样的 0.00002

如何得到这个?

Here is my string

  20.0e-6

I'm parsing it like

String Ans=Double.Parse("20.0e-6")

Now i'm getting the result like 2E-05
But the required output should be like
0.00002

How to get this?

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

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

发布评论

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

评论(3

夜未央樱花落 2024-10-13 04:20:49

获得所需结果的一种方法是使用 String.Format,如下所示:

double x = 20.0e-6;

string y = string.Format("{0:0.######}",x);

Console.WriteLine(y);

根据您的示例,这将输出值 0.00002

EDIT

I'我刚刚意识到这实际上与您的问题相反,因此为了保持答案有用,我将添加以下内容:

给定一个字符串,您可以解析为 double,然后应用与上面相同的逻辑。可能不是最优雅的解决方案,但它提供了另一种方式来获得您想要的结果。

string x = "20.0e-6";

var y = double.Parse(p);

Console.WriteLine(String.Format("{0:0.######}",y));

One way to get the result you want is to use String.Format as follow:

double x = 20.0e-6;

string y = string.Format("{0:0.######}",x);

Console.WriteLine(y);

Given your example, this outputs the value 0.00002

EDIT

I've just realised that this is actually the opposite of your question so in the aim of keeping the answer useful i'll add the following:

Given a string, you can parse as double and then apply the same logic as above. Probably not the most elegant solution however it offers another way to get the result you want.

string x = "20.0e-6";

var y = double.Parse(p);

Console.WriteLine(String.Format("{0:0.######}",y));
長街聽風 2024-10-13 04:20:48

Double.Parse 的结果是一个 Double,而不是字符串。您需要使用 ToString 从双精度数中输出一个字符串。

您还应该使用 Double.Parse 具有 NumberStyles 参数。使用 Float 值允许指数表示法:

string Ans=Double.Parse("20.0e-6", NumberStyles.Float).ToString("0.#####");

如果您不想冒异常风险(例如 InvlidCastException),则可以使用 TryParse

Double res;
if (Double.TryParse("20.0e-6", NumberStyles.Float, 
                    CultureInfo.InvariantCulture ,res))
{
  string Ans = res.ToString("0.#####");
}

The result of Double.Parse is a Double, not a string. You need to output a string from the double, using ToString.

You should also use an overload of Double.Parse that has a NumberStyles parameter. Using the Float value allows exponent notation:

string Ans=Double.Parse("20.0e-6", NumberStyles.Float).ToString("0.#####");

If you don't want to risk exceptions (InvlidCastException for example), you can use TryParse:

Double res;
if (Double.TryParse("20.0e-6", NumberStyles.Float, 
                    CultureInfo.InvariantCulture ,res))
{
  string Ans = res.ToString("0.#####");
}
小情绪 2024-10-13 04:20:48

数字相同,但如果您想修改字符串的输出,请在 ToString() 上使用格式化程序

http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

所以

String Ans=Double.Parse("20.0e-6").ToString("0.0####")

It's the same number, but if you want to modify the output of the string, use a formatter on your ToString()

http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

So

String Ans=Double.Parse("20.0e-6").ToString("0.0####")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文