-2

I Have this value: 833333333333333.33325 And need to round up with 2 decimals (should be 833333333333333.34)

If i use MidpointRounding.ToEven or AwayFromZero, i always get 833333333333333.33. How i can get 833333333333333.34 using Math in C# ?

The code is:

decimal valorArredondado = Math.Round(valorParcelado, MidpointRounding.AwayFromZero);

Where valorParcelado is 833333333333333.33325 andi need a return in decimal value.

  • 2
    You are not helping us. If you could post your code would be much better – Rekcs Jun 29 '17 at 13:47
  • 6
    Why should it be 833333333333333.34? That's Mathematically incorrect. – Zein Makki Jun 29 '17 at 13:47
  • 5
    Possible duplicate of [Rounding up to 2 decimal places in C#](https://stackoverflow.com/questions/7075201/rounding-up-to-2-decimal-places-in-c-sharp) – Liam Jun 29 '17 at 13:47
  • `833333333333333.33` is correct in your case. – erikvimz Jun 29 '17 at 13:49
  • Rounding up to 833333333333333.34 introduces an error of around 0.008, whereas rounding it to 833333333333333.33 introduces an error of around 0.003... which is surely better? – Matthew Watson Jun 29 '17 at 13:49
  • Hello. That's the code: decimal valorArredondado = Math.Round(valorParcelado, MidpointRounding.AwayFromZero); Where valorParcelado is 833333333333333.33325, and i need a return of decimal value.. – Jokes on me Jun 29 '17 at 13:53
  • @MatthewWatson: without knowing the OPs application - how do you know which is the better answer. The error may not be an issue if the requirement is to round UP to 2dp. – PaulF Jun 29 '17 at 13:55
  • The possible duplicate of Rounding up to 2 decimal places in C# not solve my problem. – Jokes on me Jun 29 '17 at 13:57
  • 1
    @PaulF It's worth asking these questions to avoid X-Y problems! – Matthew Watson Jun 29 '17 at 13:57
  • That not rounding, that's taking the ceiling value. – juharr Jun 29 '17 at 13:58
  • @user3185569: Mathematically correct does not always matter - you need to know the requirements. I have an application which requires me to collect a minimum number of samples over a specified period of time - the required sampling rate is calculated & regardless of size of the fractional part must always be rounded up - otherwise I will not collect the minimum number of samples. – PaulF Jun 29 '17 at 14:14
  • @user3185569: I think that is exactly the definition of rounding **up** with the number of decimal places specified. – PaulF Jun 29 '17 at 14:56

1 Answers1

2

You can do this:

decimal r = Math.Ceiling(d * 100m) / 100.0m;

For example:

decimal d = 833333333333333.33325m;
decimal r = Math.Ceiling(d * 100m) / 100.0m;

Console.WriteLine(d); // 833333333333333.33325
Console.WriteLine(r); // 833333333333333.34
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • Hello! Thanks for the help, and sorry for bad question :/ – Jokes on me Jun 29 '17 at 14:17
  • @Jokesonme I personally didn't think it was a bad question, I was just wondering why you needed to do non-standard rounding. :) – Matthew Watson Jun 29 '17 at 14:26
  • @MatthewWatson: See my comment above for my reason for non-standard rounding. – PaulF Jun 29 '17 at 14:29
  • @PaulF Rounding up (using ceiling) like you describe comes under my definition of standard "rounding" - but taking the ceiling value after the 2nd decimal place doesn't, hence my questions. It's definitely pretty unusual. – Matthew Watson Jun 29 '17 at 14:45
  • I guess as I'm used to working in milli, micro, nano & even pico seconds the concept of rounding up to a number of decimal places fits in with my experience. I have implemented something along the lines of your solution as an extension method for decimals allowing specification of how many decimal places to round up to. – PaulF Jun 29 '17 at 14:54