26

I have a number ("double") from int/int (such as 10/3).

What's the best way to Approximation by Excess and convert it to int on C#?

mrbm
  • 1,022
  • 1
  • 11
  • 36
markzzz
  • 44,504
  • 107
  • 265
  • 458

4 Answers4

72

Are you asking about System.Math.Ceiling?

Math.Ceiling(0.2) == 1
Math.Ceiling(0.8) == 1
Math.Ceiling(2.6) == 3
Math.Ceiling(-1.4) == -1
Doug McClean
  • 13,739
  • 4
  • 43
  • 68
11
int scaled = (int)Math.Ceiling( (double) 10 / 3 ) ;
Arne
  • 10,476
  • 3
  • 48
  • 66
EursPravus
  • 303
  • 2
  • 8
1

By "Approximation by Excess", I assume you're trying to "round up" the number of type double. So, @Doug McClean's "ceiling" method works just fine.

Here is a note: If you start with double x = 0.8; and you do the type conversion by (int)x; you get 0. Or, if you do (int)Math.Round(x); you get 1. If you start with double y = 0.4; and you do the type conversion by (int)y; you get 0. Or, if you do (int)Math.Round(y); you get 0.

Ren Wang
  • 11
  • 2
0

Consider 2.42 , you can say it's 242/100 btw you can simplify it to 121/50 .

mrbm
  • 1,022
  • 1
  • 11
  • 36