DEV Community

Discussion on: 7 useful python tips

Collapse
 
paddy3118 profile image
Paddy3118

floor, ceil but no trunc?!

# FUNCTIONS THAT GENERATE INTEGERS FROM FLOATS IN DIFFERENT WAYS.
ceil
  Return the ceiling of x as an Integral.

  This is the smallest integer >= x.

floor
  Return the floor of x as an Integral.

  This is the largest integer <= x.

trunc
  Truncates the Real x to the nearest Integral toward 0.

  Uses the __trunc__ magic method.

 ceil( 1.2) ==  2; floor( 1.2) ==  1; trunc( 1.2) ==  1
 ceil( 0.2) ==  1; floor( 0.2) ==  0; trunc( 0.2) ==  0
 ceil(   0) ==  0; floor(   0) ==  0; trunc(   0) ==  0
 ceil(-0.2) ==  0; floor(-0.2) == -1; trunc(-0.2) ==  0
 ceil(-1.2) == -1; floor(-1.2) == -2; trunc(-1.2) == -1

trunc acts like floor for n >=0 and like ceil for n < 0
Enter fullscreen mode Exit fullscreen mode
Collapse
 
sonikabaniya profile image
Sonika Baniya

seemed to have missed it out, will be mindful about this from now onwards :))