Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why returns C# Convert.ToDouble(5/100) 0.0 and not 0.05

double variable = Convert.ToDouble(5/100);

Will return 0.0 but i expected 0.05

What can / must i change to get 0.05

because the 5 in this example is a variable

like image 830
Marco Avatar asked Aug 05 '10 11:08

Marco


People also ask

Why do you return in C?

A return statement ends the execution of a function, and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can return a value to the calling function.

Why only return 0 is used in C?

These status codes will be just used as a convention for a long time in C language because the language does not support the objects and classes, and exceptions. return 0: A return 0 means that the program will execute successfully and did what it was intended to do.

Do you always have to return with C?

The C standard does not say a function must return a value or that, if it does not, the behavior is undefined.


4 Answers

5/100 is done in integer arithmetic, which yields 0 before conversion. Try

double variable = 5.0/100;

If 5 is in a variable x (of integer type), then use:

variable = (double)x/100; 

or

variable = ((double)x)/100;

to make the intent clear (thanks John!)

or

variable = x/100.0;
like image 51
Nordic Mainframe Avatar answered Oct 15 '22 05:10

Nordic Mainframe


Unlike real-world, computers treat mathematical operations a bit differently though there's no significant difference once we understand why it behaves so.

1.) Why it behaves like this ?

Note that, integers are whole numbers and the variables of type integer can only store whole numbers and can not store or recognize decimal numbers. when you say 5/100, both 5 and 100 are integer literals for the computers and it is called integer division. The result should be 0.05 but since this is an integer division the result would also be integer and as I said integers cannot store decimal point values, the trailing part after "." (decimal point) is ignored completely and hence the result is 0.

Adding more to this, though you're converting the result to double, it does not make any difference because before it is actually converted to a double the result is already 0 and it happens to convert integer 0 to double which ultimately results into 0.0.

2.) How to get your desired output ?

Other answers explain the solution very well, so I kindly request you to refer to those answers rather re-inventing the wheel for you.

Hope this helps.

like image 21
this. __curious_geek Avatar answered Oct 15 '22 06:10

this. __curious_geek


Because 5/100 in integer division is 0. You need to ensure you are doing division on doubles.

like image 23
James Avatar answered Oct 15 '22 06:10

James


5/100 is integer arithmetic. In order to have double precision one or more of the values need to be doubles.

double result = 5.0/100.0;  
double result = 5.0/100;  
double result = 5/100.0;  
double result = (double)5/100;  
double result = 5/(double)100;

or

double numerator = 5;  
double denominator = 100;  
double result = numerator / denominator;
like image 40
Jerod Houghtelling Avatar answered Oct 15 '22 05:10

Jerod Houghtelling