Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

BigDecimal#divmod

divmod(p1)

divmod(value)

Divides by the specified value, and returns the quotient and modulus as BigDecimal numbers. The quotient is rounded towards negative infinity.

For example:

Example
require 'bigdecimal'

a = BigDecimal("42")
b = BigDecimal("9")

q, m = a.divmod(b)

c = q * b + m

a == c  #=> true

The quotient q is (a/b).floor, and the modulus is the amount that must be added to q * b to get a.

See also #div #_dump #coerce #dup also in 3.3 · 3.1 · 3.0