How to convert a double 2.0 to string "2.0"

  • Thread starter Thread starter Jianwei Sun
  • Start date Start date
J

Jianwei Sun

Hi, guys,

If I simply do a double d = 2.0 ,and then s=d.ToString() , it will trim
the .0 off and return me 2, is there a good way to keep .0 and return me
2.0.

Thanks,
 
Use the formatting optin in the ToString() method:

double d1 = 2.0;
double d2 = 2.01;

Trace.WriteLine( d1.ToString("#,###.0") );
Trace.WriteLine( d1.ToString("#,###.00") );
Trace.WriteLine( d2.ToString("#,###.0") );
Trace.WriteLine( d2.ToString("#,###.00") );
Trace.WriteLine( d2.ToString("#,###.000") );
 
The ToString method is overloaded and accepts parameters to specify the
format. For example ToString("#.0")

See the docs.

--
Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
Back
Top