Click here to Skip to main content
15,895,777 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I have a problem which I want to Discuss with you, may be you have solution for me, Actually in Point of Sale, I have Decimal Amounts at checkout and I want to Round it off like,

Example:
CASE # 1:

If Amount/Value is Greater than '0' or Less than '4' I need result as '0' like
Amount is like 102.00 OR 103.45 OR 101.66 OR 104 I want Result after Round-Off is 100

CASE # 2:

If Amount/Value is Greater than '4' or Less than '6' I need result as '5' like
Amount is like 104.12 OR 104.00 OR 105.54 OR 105.96 I want Result after Round-Off is 105

CASE # 3:

If Amount/Value is Greater than '6' or Less than '10' I need result as '10' like
Amount is like 106.96 OR 107.00 OR 108.72 OR 109.89/b> I want Result after Round-Off is 110

Please Explain with Proper Code, so It will easy for me to understand it.
Thanks You
Posted
Updated 16-Aug-12 6:16am
v2

I seriously doubt you need to round anything, ever. This is very, very rarely needed. It looks like you want somewhat different thing: you only need to present some value in a form of a string as a rounded value, for presentation purposes only; and this is a different story. This is not rounding per se but formatting, much more appropriate thing.

Please use System.Decimal.ToString with appropriate format specifier:
http://msdn.microsoft.com/en-us/library/fzeeb5cd.aspx[^],
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^].

Basically, you would need to use the custom format with '0', '#' and '.' placeholders (the last link). Behind the scene, your data will be properly rounded, but without any risk of rounded values sneaking into the calculations, so it's not only easier, but also much safer.

—SA
 
Share this answer
 
Create you own round method

C#
private int Round(Decimal x)
{


//decimal x = 100;
            string strTemp = x.ToString();
            strTemp = strTemp.Substring(0, strTemp.IndexOf("."));
            if (Convert.ToInt32(strTemp.Substring(strTemp.Length - 1)) < 4)
            {
              strTemp=  strTemp.Replace(strTemp.Substring(strTemp.Length - 1), "0");
            }
            else if (Convert.ToInt32(strTemp.Substring(strTemp.Length - 1)) > 4 && Convert.ToInt32(strTemp.Substring(strTemp.Length - 1)) < 6)
            {
              strTemp=  strTemp.Replace(strTemp.Substring(strTemp.Length - 1), "5");
            }
            else if (Convert.ToInt32(strTemp.Substring(strTemp.Length - 1)) > 6 && Convert.ToInt32(strTemp.Substring(strTemp.Length - 1)) < 9)
            {
              int temp =  Convert.ToInt32( ((x / 100) / 10) + 1);
              if (temp < 10)
              {
               strTemp=   strTemp.Replace(strTemp.Substring(strTemp.Length - 2), temp.ToString() + "0");
              }
              else
              {
                strTemp=  strTemp.Replace(strTemp.Substring(strTemp.Length - 3), Convert.ToString(((x / 100) + 1)) + "00");
              }
            }
            return Convert.Toint32(strTemp);

}
 
Share this answer
 
v2
Comments
ridoy 16-Aug-12 3:52am    
good one.
pradiprenushe 16-Aug-12 3:55am    
thanks

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