7

I have very stupid issue i am simply taking input of on double and adding it to other double which is already declared and assign a value but sum is not showing floating point

double d = 4.0;
// Getting second double from user
double numDouble = Double.Parse(Console.ReadLine());

//Printing double number :
Console.WriteLine(d + numDouble);

result is always 4.0 + 2.0 = 6 but i want 6.0 any idea

iAhmed
  • 6,556
  • 2
  • 25
  • 31
Dev
  • 307
  • 1
  • 2
  • 12
  • Take a look at MSDN for more information: https://msdn.microsoft.com/en-us/library/kfsatb94(v=vs.110).aspx – Kevin Mar 29 '16 at 09:23

3 Answers3

12

Math says, that

 6 = 6.0 = 6.00 = 6.000 = ...

so what you want is a representation of double value as a string:

 // F1: - one digit after decimal point
 Console.WriteLine((d + numDouble).ToString("F1"));
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • Thanks it worked in my case so i was missing to convert it to string and formatting that string – Dev Mar 29 '16 at 09:33
4
Console.WriteLine("{0:F1}", d + numDouble);
karthik
  • 74
  • 9
  • 21
  • 45
0

Here's another way to format your string for the output.

Console.WriteLine(String.Format("{0:0.0}", (d + numDouble)));

Credits

Didix
  • 567
  • 1
  • 7
  • 26