Flowdock
round(p1 = v1, p2 = {}) public

Returns float rounded to the nearest value with a precision of ndigits decimal digits (default: 0).

When the precision is negative, the returned value is an integer with at least ndigits.abs trailing zeros.

Returns a floating point number when ndigits is positive, otherwise returns an integer.

1.4.round      #=> 1
1.5.round      #=> 2
1.6.round      #=> 2
(-1.5).round   #=> -2

1.234567.round(2)   #=> 1.23
1.234567.round(3)   #=> 1.235
1.234567.round(4)   #=> 1.2346
1.234567.round(5)   #=> 1.23457

34567.89.round(-5)  #=> 0
34567.89.round(-4)  #=> 30000
34567.89.round(-3)  #=> 35000
34567.89.round(-2)  #=> 34600
34567.89.round(-1)  #=> 34570
34567.89.round(0)   #=> 34568
34567.89.round(1)   #=> 34567.9
34567.89.round(2)   #=> 34567.89
34567.89.round(3)   #=> 34567.89

If the optional half keyword argument is given, numbers that are half-way between two possible rounded values will be rounded according to the specified tie-breaking mode:

  • :up or nil: round half away from zero (default)

  • :down: round half toward zero

  • :even: round half toward the nearest even number

    2.5.round(half: :up)      #=> 3
    2.5.round(half: :down)    #=> 2
    2.5.round(half: :even)    #=> 2
    3.5.round(half: :up)      #=> 4
    3.5.round(half: :down)    #=> 3
    3.5.round(half: :even)    #=> 4
    (-2.5).round(half: :up)   #=> -3
    (-2.5).round(half: :down) #=> -2
    (-2.5).round(half: :even) #=> -2
Show source
Register or log in to add new notes.