2
Answers

Round for Decimal

Photo of jszym20

jszym20

20y
2.9k
1
Hi, I have problem. I writed code Decimal.Round(System.Convert.ToDecimal( 8930.305), 2) it return 8930.30, but Decimal.Round(System.Convert.ToDecimal( 8930.3051), 2) return 8930.31, If I write in SQL select Round(8930.305, 2) from dual; it return 8930.31 So what can I do? I must to have same result. I thing that Decimal.Round(System.Convert.ToDecimal( 8930.305), 2) should return 8930.31. What can I do? I have two different result. Please help me, Asia

Answers (2)

0
Photo of jszym20
NA 20 0 20y
Thanks, :-)
0
Photo of joy.simpson
NA 228 0 20y
You need to write your own function. The built-in Round function uses banker's rounding to round to the nearest EVEN digit when the LSD is a 5. Here is one solution to always rounding up when the least significant digit is a 5: static void Main(string[] args) { double num; int place; string snum; Console.Write("Enter positive or negative number to round: "); snum = Console.ReadLine(); if (snum == "") snum = "0"; num = Convert.ToDouble(snum); Console.Write("Number of places to round right or left (-) of decimal: "); snum = Console.ReadLine(); if (snum == "") snum = "0"; place = Convert.ToInt32(snum); Console.WriteLine("\nThe rounded number is {0}", roundnum(num, place)); Console.WriteLine(); } public static double roundnum(double num, int place) { double n; n = num * Math.Pow(10, place); n = Math.Sign(n) * Math.Abs(Math.Floor(n + .5)); return n / Math.Pow(10, place); }