Click here to Skip to main content
15,886,833 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i have these number
10.02
10.03
10.04
10.05
10.06
10.07
10.08
10.09
10.10
now i want to round up these number just like
10.02=>10
10.03=>10
10.04=>10.05
10.05=>10.05
10.06=>10.05
10.07=>10.05
10.08=>10.10
10.09=>10.10
there is any function in c# please tell me how can i do this

What I have tried:

i already try this code but its not show result according to my requirements

double n= Math.Round(10.04/ 5.0) * 5;

it showing result 10 but i want result n=10.05
please help me
Posted
Updated 17-Oct-19 0:11am
Comments
jimmson 17-Oct-19 5:54am    
Why would 10.04 round to 10.05? It doesn't make any sense.
Fahid Zahoor 17-Oct-19 6:01am    
some time customer bil amount is 10.04 but we take 10.05 if custome bil is 10.02 then we take only 10.00 can you understand
jimmson 17-Oct-19 6:11am    
Look at the https://docs.microsoft.com/en-us/dotnet/api/system.math.round?view=netframework-4.8 to get some info on Math.Round method and to see your options. However, changing 10.04 to 10.05 has nothing to do with rounding. You will need to implement your own algorithm.
Fahid Zahoor 18-Oct-19 14:04pm    
thanks man for your response now i write my own algorithm when i complete then i also share with you so explain me my algorithm writing process are professional or not.
George Swan 17-Oct-19 12:25pm    
As your rounding method is somewhat unconventional you will probably need a lookup table to return a small number (epsilon) that can be positive or negative that's added to the number under consideration to produce the rounding that you require. To do this you need to get the last digit of your number and use that as the index value for the lookup table.Your numbers have 2 fractional digits so you can get the last digit by multiplying the number by 10 to the power of 2 =100 and taking the modulus 10 of that number. When you do the multiplication, round the result to return a number with no factional digits.

1 solution

Quote:
it showing result 10 but i want result n=10.05
please help me

So you tried 1 thing, it is not the result you expect, so you abend and you call for help.
In the job of programming, there are 2 things debug and experiment.
If you experiment, you will see that your formula gives results 0, 5, 10, 15, 20 ...
Try values different than 5 and see what you get, it shouldn't be long before you find the correct value.

It is really not complicated.
This experiment is trial and error learning, it usually improve your learning curve, but you have to do the job yourself.
 
Share this answer
 
v2

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