Why returns C # Convert.ToDouble (5/100) 0,0, but not 0,05

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

Will return 0.0, but I was expecting 0.05

What can / needs to be changed to get 0.05

because 5 in this example is a variable

+7
double c # rounding
source share
7 answers

5/100 is performed in integer arithmetic, which gives 0 before the conversion. Try

 double variable = 5.0/100; 

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

 variable = (double)x/100; 

or

 variable = ((double)x)/100; 

to make the intention clear (thanks to John!)

or

 variable = x/100.0; 
+18
source share

Unlike the real world, computers process mathematical operations in a slightly different way, although there is no significant difference after we understand why this is so.

1.) Why does he act like that?

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

Adding more to this, although you are converting the result to double, it does not matter, because before it is actually converted to double, the result is already 0, and the integer 0 is converted to double, which ultimately leads to 0, 0 .

2.) How to get the desired result?

Other answers explain this solution very well, so I kindly ask you to refer to these answers, and not reinvent the wheel for you.

Hope this helps.

+4
source share

Since 5/100 in integer divisions is 0. You need to make sure that you are doing the division by paired.

+2
source share

5/100 - integer arithmetic. To have double precision, you must double one or more values.

 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; 
+2
source share

double variable = 5D / 100D;

+1
source share

so here is my revised "reviewed" answer ...

since we don’t know that the β€œtype” of the variable comes as a numerator, we would have to use Double.TryParse. In the lab, we could cook something like this:

 var numerator = "5"; // let make it string to prove the point double parsedNumerator; int denominator = 100; // this could well be a constant double result; if(Double.TryParse(numerator, out parsedNumerator)) { // notice no casting or convert fluff result = parsedNumerator/denominator; // do something with the result } else { // warn that the numerator is doo-lallie } 

now hiding under the table - just in case, I forgot something else obvious !! :)

Jim

0
source share

Marco

try this instead:

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

Jim

[late edit] - as stated (by silent voters :)), using Convert is redundant. Obviously, this should be done in the same way as other records. I admit this, but leave it above, as an example of how to quickly retreat when answering a question in a hurry ... be careful, be your master!

In addition, since we do not know whether the variable will come from a string or a number, it should also consider tryparse, etc. to number 1 before doing arithmetic.

-3
source share

All Articles