Click here to Skip to main content
15,898,036 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends,
Good Evening
I want to covert the decimal values to the nearest decimal number i.e
Suppose my value is

87.6408
then O/P should be either 60 if below 5 and 70 above 5
87.60 or 87.70
Any help will be appreciated. I have tried
noo = Math.Round(noo, 2,MidpointRounding.ToEven);and also
noo = Math.Round(noo, 2,MidpointRounding.AwayFromZero);This is working but not as I want. Please help.Thank You
Posted

C#
double noo = Convert.ToDouble(txttotalamount.Text);
noo = Math.Round(noo, 1);
                  
string numString = string.Format("{0:###0.00}", Math.Round(noo, 2, MidpointRounding.AwayFromZero));
 
Share this answer
 
Comments
ProEnggSoft 16-Feb-12 20:32pm    
If you know the solution, why have you posted the question?
This is same as Solution 5. You have to post it under solution 5 as your comment. You cannot take another solution and post as your solution.
try:

roundnoo = Math.Round(noo, 2); 


or
 
Share this answer
 
Convert the decimal ToString("#.##")
this leads to "12345.67"
Read the last char from string and decide if it should be down or up (60 or 70)
 
Share this answer
 
Comments
Santoshrohin 16-Feb-12 6:25am    
Will you please elaborate sir.Please
Try,

noo = Math.Round(noo, 1, MidpointRounding.ToEven)
 
Share this answer
 
The way you describe your problem already holds the answer. You want to do rounding to 87.60 or 87.70 for example. You do rounding to two decimal values but actually you want one decimal, meaning you would like to round to 87.6 or 87.7 (the extra zero would only be for display and has no meaning for the number itself)

Good luck!
 
Share this answer
 
Comments
Santoshrohin 16-Feb-12 6:39am    
Ya that's right but I am converting this value in word format so while printing i need to Print .60 or .70 to sixty or seventy paise.Please help me
E.F. Nijboer 16-Feb-12 7:26am    
Just use this to format it for display: String.Format("{0:0.00}", 87.6); -> 87.60
You may try
C#
string numString = string.Format("{0:####0.00}",Math.Round(num,2, MidpointRounding.AwayFromZero));


0.00 in the format string ensures that .6 is printed as 0.60.

If your problem is solved, you can accept and vote the solution, so that other users can know that the question is answered, and they can view it if required. Otherwise please post your queries.

PES
 
Share this answer
 
Comments
Santoshrohin 16-Feb-12 7:08am    
Thank you sir it is working as per my requirement for big decimal number but when the I/P is like 112.36
then it the O/P is Rupees One Hundred Twelve and Thirty Six Paise Only ,I want the paise should be forty instead of Thirty Six. Please help
ProEnggSoft 16-Feb-12 7:15am    
Then put Math.Round(num,1,MidpointRounding.AwayFromZero), the 112.36 will be rounded to 112.4 and printed as 112.40
Santoshrohin 16-Feb-12 7:31am    
Thank you sir.
ProEnggSoft 16-Feb-12 7:44am    
If your requirement is fulfilled then you may accept and vote the solution, so that other users can see that the question is already answered. Otherwise please post your queries.

PES

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