2

Possible Duplicate:
c# - How do I round a decimal value to 2 decimal places (for output on a page)

how to return decimal with long rest after the point like that:

3.786444499963

to this:

3.787

its not just cut the points it also round the rest of the number

Community
  • 1
  • 1
roy.d
  • 1,030
  • 2
  • 16
  • 33
  • 4
    You know that it's the wrong way to round, right? The right way is to simply look at the fourth decimal digit (in this case) – xanatos Mar 15 '12 at 14:13
  • @xanatos if you're doing banker's rounding, you need to look at the fourth digit and, if it's 5, see whether there are any non-zero digits after the fourth. – phoog Mar 15 '12 at 14:50
  • @phoog That isn't banker's rounding. Taken from [msdn](http://msdn.microsoft.com/en-us/library/zy06z30k.aspx): If the value of the first digit in d to the right of the decimals decimal position is 5, the digit in the decimals position is rounded up if it is odd, or left unchanged if it is even. If the precision of d is less than decimals, d is returned unchanged. The behavior of this method follows IEEE Standard 754, section 4. This kind of rounding is sometimes called rounding to nearest, or banker's rounding. – xanatos Mar 15 '12 at 14:55
  • @xanatos That description is incorrect. With banker's rounding, `0.5` rounds to zero, but `0.5000001` rounds to 1. In other words, it's not determined by the first digit to the right; rather, it's determined by the magnitude of the decimal amount: if it's greater than half, round up; if it's less than half, round down; if it's equal to half, round to the nearest even number. – phoog Mar 15 '12 at 15:02
  • @phoog You are right... The funny thing is that they implemented it correctly :-) – xanatos Mar 15 '12 at 15:06
  • @xanatos it's not the first time MSDN has been incorrect. I submitted a "community content" entry explaining the error. – phoog Mar 15 '12 at 15:16

3 Answers3

5
Math.Ceiling(3.786444499963 * 1000) / 1000;
Vano Maisuradze
  • 5,829
  • 6
  • 45
  • 73
  • thanks both answers works: Math.Ceiling(3.786444499963 * 1000) / 1000; and Math.Round(3.786444499963m + 0.0005m, 3); – roy.d Mar 15 '12 at 14:34
4

But the generally accepted rounding of 3.786444499963 to three decimal places is 3.786. Why do you think otherwise?

Thus:

var round = Math.Round(3.786444499963m, 3, MidpointRounding.AwayFromZero);
Console.WriteLine(round == 3.786m); // prints true

If you want it to ALWAYS round up:

var round = Math.Round(3.786444499963m + 0.0005m, 3);
Console.WriteLine(round == 3.787m); // prints true

Do you see what I did there? I added 0.0005m to the input before using Math.Round. In general, to round x to n decimal places,

var round = Math.Round(x + 5m * Convert.ToDecimal(Math.Pow(10, -n - 1)), n);

Or, perhaps, to avoid the ugly double/decimal conversion:

int k = 1;
decimal value = 5m;
while(k <= n + 1) { value /= 10m; k++; }
var round = Math.Round(x + value, n);

There's an edge case you need to be aware of. What happens to 3.786? Should it be rounded up to 3.787 or remain at 3.786? You haven't specified what you want exactly, so I'll leave this edge case to you.

jason
  • 236,483
  • 35
  • 423
  • 525
0
    RoundUp(3.786444499963M, 3);

    static decimal RoundUp(decimal dec, int precision)
    {
        decimal rounder = (decimal)(0.5 * Math.Pow(10, -precision));
        return Math.Round(dec + rounder, precision);
    }
Saeb Amini
  • 23,054
  • 9
  • 78
  • 76
  • 2
    It will return 3.786. Math.Round doesn't "cascade" from last digit onward. It simply looks at digit position + 1 – xanatos Mar 15 '12 at 14:12
  • i need it to be round up – roy.d Mar 15 '12 at 14:16
  • @roy.d: There are 3 main ways to round. For example rounding to no decimals: with Round the aproximation is taken to the closest integer (0.3 -> 0; 0.7 -> 1); with Ceil the aproximation is always the closest higher integer (0.3 -> 1; 0.7 -> 1) and with Floor the aproximation is always the closest lower integer (0.3 -> 0; 0.7 -> 0). Which one of these do you need? – Răzvan Flavius Panda Mar 15 '12 at 14:47