format numeric to string - decimal places

M

Mark

I have a Decimal or a Double - your choice. I'd like to print the number as
string with a specified number of decimal places. Let's say my number
110.5, and I'd like to capture it in a string with 3 decimal places:
"110.500". How do you do this? My method below isn't working so hot.

Decimal dcTotal = 110.5;
string strMyString = (Decimal.Round(dcTotal, 3)).ToString();
//Do something with this string

Thanks in advance!

Mark
 
M

mikeb

Mark said:
I have a Decimal or a Double - your choice. I'd like to print the number as
string with a specified number of decimal places. Let's say my number
110.5, and I'd like to capture it in a string with 3 decimal places:
"110.500". How do you do this? My method below isn't working so hot.

Decimal dcTotal = 110.5;
string strMyString = (Decimal.Round(dcTotal, 3)).ToString();
//Do something with this string

dcTotal.ToString("0.000")

To be honest, I find it difficult to locate this information in the docs
- it takes a bit of digging past relatively useless stuff about
NumberFormatInfo and CultureInfo classes to get to the basic, predefined
format characters I want.

This information happens to be in the docs under the heading "Custom
Numeric Format Strings".

Note to MS: please put links to the standard and custom formatting
characters for the numeric and DataTime types in the document pages for
ToString() and String.Format().
 
G

Guest

"> > I have a Decimal or a Double - your choice. I'd like to print the
number as
....
To be honest, I find it difficult to locate this information in the docs
I agree on that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top