12,818,964 members (26,829 online)
Rate this:
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 15-Aug-12 22:19pm
Updated 16-Aug-12 7:16am
v2

Rate this:

## 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
Rate this:

## 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);

}```
v2
ridoy 16-Aug-12 3:52am

good one.

thanks

Top Experts
Last 24hrsThis month
 OriginalGriff 205 Jochen Arndt 170 Richard Deeming 125 Karthik Bangalore 95 Peter Leow 75
 OriginalGriff 4,607 Graeme_Grant 3,814 Karthik Bangalore 3,501 Jochen Arndt 2,571 ppolymorphe 2,447