Click here to Skip to main content
12,293,547 members (61,021 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# .NET
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 15-Aug-12 21:19pm
Edited 16-Aug-12 6:16am
Wes Aday104.2K
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Create you own round method

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);
 
}
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160518.1 | Last Updated 16 Aug 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100