I need to add double values and display it result. But the resulting value was wrong.
class Program
{
static void Main(string[] args)
{
int i = 4;
double d = 4.0;
string s = "HackerRank ";
int i1 = 12;
double d1 = 4.0;
string s1 = "is the best place to learn and practice coding!";
int sum = 0; string s2 = string.Empty;
sum = sum+i + i1;
s2 = s2 + s + s1;
Console.WriteLine(sum);
Console.WriteLine("{0}+{1}={2}",d,d1,(d+d1).ToString());
Console.WriteLine(s2);
Console.ReadLine();
}
}
My expected output is 4.0+4.0=8.0
But the output value is roundoff. Can anyone provide the reason and solution for this question?
if you want to show the output in x.xx format you can use:
Console.WriteLine("{0:F2}+{1:F2}={2:F2}",d,d1,d+d1);