Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
4.04/5 (4 votes)
See more:
Hi Geeks,

Just a small doubt,

result=210900
C#
double value = Convert.ToDouble(result / 10000);

C#
value=21.0 
I need to get 21.09

How to get this?

Thanks
Posted
Comments
Philippe Mori 2-Aug-14 9:09am    
You have to learn primitive types rules... In C/C++/C#, 201900 / 10000 is an integer division.

1.When both operands are integer the results will be computed as integer (so without any decimals), then the final result will be converted to double.

2.The solution is to use variable of type double and then you could use Math.Round() in order to get as many decimal you wanted like in the next example:

C#
double value = Math.Round(result / 10000.0, 2);
 
Share this answer
 
This way
C#
int result = 210900;
double value = result / 10000.0;
 
Share this answer
 
Comments
Sriram Ramachandran 31-Jul-14 2:29am    
Thanks.. It worked. But how this is working??? Why 10000.0??
CPallini 31-Jul-14 2:34am    
10000.0 is a double constant. It forces the promotion of result to double before the division operator is applied.
Raul Iloc 31-Jul-14 2:34am    
When both operands are integer the results will be computed as integer (so without any decimals), then the final result will be converted to double.
 
Share this answer
 
v2
Comments
Philippe Mori 2-Aug-14 9:01am    
Your example suffer the samme problem as OP example.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900