-2
double paintneeded = area/450;

When I run the above, I get the output as 0.444444444, but I wanted only 0.44.

I tried this, but it throws an error:

double paintneeded = String.Format("{0:0.00}", (area/450));

Console.WriteLine("Number of Gallons paint needed:\t{0}", paintneeded);

How do I use String.Format in this expression? Or can I use it in the Console.WriteLine? If so, how do I implement it?

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
user3648850
  • 19
  • 1
  • 2
  • 4

2 Answers2

2
Console.WriteLine("Number of Gallons paint needed:\t{0:F2}", area/450.0);
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
0

First way, convert to string:

double area = 150;
string paintneeded = string.Format("{0:0.00}", (area/450d)).ToString();

Console.WriteLine("Number of Gallons paint needed:\t{0}", paintneeded); 

Second way, convert to double:

double area = 150;
double paintneeded = area/450d;

Console.WriteLine("Number of Gallons paint needed:\t{0:0.00}", paintneeded);
Magistr_AVSH
  • 368
  • 2
  • 12
  • Thanks Magistr_AVSH I was able to figure it out, but now iam struggling to include thousands separator and the digits after decimal together in one – user3648850 May 18 '14 at 06:05
  • salestax = Convert.ToDouble(string.Format("{0:0,0.00}",((subtotal * 8.25) / 100))); //o/p should be 1,749.00 but I got it as 1749 also if I want to have the o/p as 100.00 what needs to be changed in the syntax?? – user3648850 May 18 '14 at 06:07
  • Why you convert string to double? Of course you are lose formatting. Do something like this double area = 15000.179; Console.WriteLine(area.ToString("N", CultureInfo.InvariantCulture)); [netfiddle](https://dotnetfiddle.net/fL1W4B) – Magistr_AVSH May 18 '14 at 06:21