Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing a double with 2 digit precision

Tags:

c#

c#-4.0

How do I convert a double to have precision of 2 places?

For ex:

    double x = 1.00d;           
    Console.WriteLine(Math.Round(x,2,MidpointRounding.AwayFromZero));
//Should output 1.00 for me, but it outputs just 1.

What I'm looking for is to store 1.00 in a double variable which when printed to console prints 1.00.

Thanks, -Mike

like image 234
Mike Avatar asked Nov 28 '22 09:11

Mike


1 Answers

"x" stores the number - it doesn't pay attention to the precision.

To output the number as a string with a specific amount of precision, use a format specifier, ie:

Console.WriteLine(x.ToString("F2"));
like image 89
Reed Copsey Avatar answered Dec 16 '22 02:12

Reed Copsey