将double更改为string后的字符串格式。 [英] Format of a string after change of double to string.

查看:104
本文介绍了将double更改为string后的字符串格式。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我有一个双倍,我想将其更改为字符串,如下所示:



Hello,

I have a double, and I want to change it to a string, like this:

double value;
string myString = value.toString();





当值是点后少于4位的数字时,它可以正常工作。

例如,

如果值为0,myString将为0.

如果值为0.01,myString将为0.01。



但是在值的情况下在点之后有4位或更多位数,myString是用浮点创建的(例如,1E-05)。



我希望以格式创建myString在点之后的任意位数为0.0000000X,并且从不使用1E-0X方法。

我也想让myString保持尽可能短,例如当值为0时,我想要myString为0(而不是0.000000)。



我该怎么办?

谢谢



When value is a number with less than 4 digits after the point, it works fine.
For example,
if value is 0, myString will be 0.
if value is 0.01, myString will be 0.01.

But in cases when value has 4 or more digits after the point, myString is created with a floating point (for example, 1E-05).

I want myString to be created in a format of 0.0000000X for any number of digits after the point, and never use the 1E-0X method.
I also want to keep myString as short as possible, for exmaple when the value is 0, i want myString to be 0 (and not 0.000000).

How can I do it?
Thanks

推荐答案

double value;
decimal d= Convert.ToDecimal(value);
string myString = d.toString();


浏览以下MSDN链接。解释了所有必需的格式。

http:// msdn.microsoft.com/en-us/library/dwhawy9k(VS.100).aspx [ ^ ]
Go through the following MSDN link. All the required formats are explained.
http://msdn.microsoft.com/en-us/library/dwhawy9k(VS.100).aspx[^]


您可以通过阅读格式说明符来完成:

http://msdn.microsoft.com/en-us/library/system。 double.tostring.aspx [ ^ ],

http://msdn.microsoft。 com / en-us / library / dwhawy9k.aspx [ ^ ],

http:// msdn。 microsoft.co m / en-us / library / 0c899ak8.aspx [ ^ ]。



-SA
You can do it by reading on format specifiers:
http://msdn.microsoft.com/en-us/library/system.double.tostring.aspx[^],
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^].

—SA


这篇关于将double更改为string后的字符串格式。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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