Click here to Skip to main content
15,904,288 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this code
C#
decimal total = 2m;
decimal discount = 0.2m;
decimal disPercent = discount / total;


disPercent value should be 0.0909090909090909
but I get 0.1
what should I do to get 0.0909090909090909?


note that the values are for example, I always get them from database as decimal values

What I have tried:

C#
decimal total = 2m;
decimal discount = 0.2m;
decimal disPercent = decimal.Divide(discount, total);

not working
Posted
Updated 26-Nov-16 0:51am
v2

Why do you think that 0.2 / 2.0 is 0.0909090909...
The maths I was taught as a child says different:
0.2 / 2.0 == 2.0 / 20.0 == 1.0 / 10.0 == 0.1
 
Share this answer
 
Comments
M. Daban 26-Nov-16 7:01am    
ohmy bad, sorry I did all my calculations on 2.2/0.2 not 2/0.2
it's alzheimer :(
OriginalGriff 26-Nov-16 7:12am    
I know the feeling well! :laugh:
0.2 / 2 does, in fact, equal 0.1. I am thinking that your question may have needed to include the 0.2 in the total, so 2.0 is paid, 0.2 is discount, and 2.2 is total. Then if you divide 0.2 by 2.2, you will get your expected result of 0.0909 recurring (effectively 1/11).
 
Share this answer
 
v2
Dividing decimals already returns an accurate value (more accurate than doing ANY math with floating point values).
 
Share this answer
 

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