bigdecimal provides arbitrary-precision decimal arithmetic in Ruby, essential for financial calculations and any domain requiring exact decimal representation. Unlike Ruby's Float type which uses binary floating-point arithmetic prone to rounding errors, BigDecimal performs decimal arithmetic with user-specified precision, eliminating common floating-point issues. The gem (formerly in Ruby's standard library) ensures calculations involving money, percentages, or measurements maintain exact decimal precision.
The library represents numbers in decimal form with configurable precision, supporting the full range of arithmetic operations including addition, subtraction, multiplication, division, and mathematical functions. It handles precision control explicitly, allowing developers to specify significant digits and rounding modes according to business rules or standards. BigDecimal provides accurate comparison operations, conversion to and from other numeric types, and serialization for storage. The gem implements IEEE 854 decimal arithmetic specifications, ensuring consistent behavior across platforms and use cases.
bigdecimal is critical for financial applications, accounting systems, e-commerce platforms, and any domain where decimal precision errors are unacceptable. It's used in payment processing, tax calculations, currency conversions, and scientific calculations requiring exact decimal representation. Applications handling money should always use BigDecimal rather than Float to avoid accumulating rounding errors that, while tiny, can cause reconciliation problems, incorrect tax calculations, or regulatory compliance issues. The gem's precise decimal arithmetic makes it the foundation for reliable financial computations in Ruby.
gem install bigdecimal Run this gem instantly in your browser without any installation:
Open in RunRuby.dev →All 51 versions available for installation