Number Rounding/Formating

  • Thread starter Thread starter Eric
  • Start date Start date
E

Eric

Hi,

In my application I need to round a number to 3 decimal places. I am using
the Math.Round method which works fine. The problem I am running into is
when the number has no decimals like 5 because I need to show it as 5.000.
How can I accomplish this?

Thanks
 
In my application I need to round a number to 3 decimal places. I am using
the Math.Round method which works fine. The problem I am running into is
when the number has no decimals like 5 because I need to show it as 5.000.
How can I accomplish this?

Use a custom format string, e.g.

Console.WriteLine ("{0:0.000}", 5.5);
 
I do not want to use WriteLine. I want to do it inside my business objects.
Is there another way?

Thanks
 
I do not want to use WriteLine. I want to do it inside my business objects.
Is there another way?

Yes - just use String.Format using the same format string, or
yourDouble.ToString("0.000"). My code was only demonstrating the format
string to use.
 
Eric said:
I do not want to use WriteLine. I want to do it inside my business objects.
Is there another way?

the Double, Float, and Decimal datatypes' ToString() method has an
overload that takes a format string:

myVal.ToString( "0.000");

 
Back
Top