Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems Adding two double numbers

Tags:

c#

console

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

like image 754
Dev Avatar asked Mar 29 '16 09:03

Dev


People also ask

What is a double of 13?

When you double a number, the answer is always even. Example 1 => To double 13 => Firstly, partition 13 => 10 and 3.

What is doubling for kids?

To get a double of a number, we add the same number to itself. For example, double of 2 is 2 + 2 = 4.

What is a double number of 12?

Double of 12 = 12 + 12 = 24.


2 Answers

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"));
like image 78
Dmitry Bychenko Avatar answered Sep 28 '22 05:09

Dmitry Bychenko


Console.WriteLine("{0:F1}", d + numDouble);
like image 38
karthik Avatar answered Sep 28 '22 05:09

karthik