Logo

Programming-Idioms

  • C++
  • Haskell
  • Perl
  • Ruby

Idiom #80 Truncate floating point number to integer

Declare integer y and initialize it with the value of floating point number x . Ignore non-integer digits of x .
Make sure to truncate towards zero: a negative x must yield the closest greater integer (not lesser).

y = x.to_i

Synonyms are truncate and to_int
int y = static_cast<int>(x);
y = truncate x
my $y = int($x);
Y : constant Integer := Integer (Float'Truncation (X));

New implementation...