Method: Time#round
- Defined in:
- time.c
#round(ndigits = 0) ⇒ Time
Returns a new Time object whose numeric value is that of self, with its seconds value rounded to precision ndigits:
t = Time.utc(2010, 3, 30, 5, 43, 25.123456789r)
t # => 2010-03-30 05:43:25.123456789 UTC
t.round # => 2010-03-30 05:43:25 UTC
t.round(0) # => 2010-03-30 05:43:25 UTC
t.round(1) # => 2010-03-30 05:43:25.1 UTC
t.round(2) # => 2010-03-30 05:43:25.12 UTC
t.round(3) # => 2010-03-30 05:43:25.123 UTC
t.round(4) # => 2010-03-30 05:43:25.1235 UTC
t = Time.utc(1999, 12,31, 23, 59, 59)
t # => 1999-12-31 23:59:59 UTC
(t + 0.4).round # => 1999-12-31 23:59:59 UTC
(t + 0.49).round # => 1999-12-31 23:59:59 UTC
(t + 0.5).round # => 2000-01-01 00:00:00 UTC
(t + 1.4).round # => 2000-01-01 00:00:00 UTC
(t + 1.49).round # => 2000-01-01 00:00:00 UTC
(t + 1.5).round # => 2000-01-01 00:00:01 UTC
Related: Time#ceil, Time#floor.
4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 |
# File 'time.c', line 4557 static VALUE time_round(int argc, VALUE *argv, VALUE time) { VALUE ndigits, v, den; struct time_object *tobj; if (!rb_check_arity(argc, 0, 1) || NIL_P(ndigits = argv[0])) den = INT2FIX(1); else den = ndigits_denominator(ndigits); GetTimeval(time, tobj); v = w2v(rb_time_unmagnify(tobj->timew)); v = modv(v, den); if (lt(v, quov(den, INT2FIX(2)))) return time_add(tobj, time, v, -1); else return time_add(tobj, time, subv(den, v), 1); } |