Class

BigDecimal extends the native Integer class to provide the to_d method.

When you require the BigDecimal library in your application, this methodwill be available on Integer objects.

Add double dispatch to Integer

This class is the basis for the two concrete classes that hold whole numbers, Bignum and Fixnum.

Class Methods

Iterates the given block over all prime numbers.

See Prime#each for more details.

Re-composes a prime factorization and returns the product.

See Prime#int_from_prime_division for more details.

Instance Methods
An alias for to_i

Returns a string containing the character represented by the int‘s value according to encoding.

65.chr    #=> "A"
230.chr   #=> "\346"
255.chr(Encoding::UTF_8)   #=> "\303\277"

Returns 1.

Iterates the given block, passing decreasing values from int down to and including limit.

If no block is given, an Enumerator is returned instead.

5.downto(1) { |n| print n, ".. " }
print "  Liftoff!\n"
#=> "5.. 4.. 3.. 2.. 1..   Liftoff!"

Returns true if int is an even number.

An alias for to_i

Returns the greatest common divisor (always positive). 0.gcd(x) and x.gcd(0) return abs(x).

2.gcd(2)                    #=> 2
3.gcd(-7)                   #=> 1
((1<<31)-1).gcd((1<<61)-1)  #=> 1

Returns an array; [int.gcd(int2), int.lcm(int2)].

2.gcdlcm(2)                    #=> [2, 2]
3.gcdlcm(-7)                   #=> [1, 21]
((1<<31)-1).gcdlcm((1<<61)-1)  #=> [1, 4951760154835678088235319297]

Since int is already an Integer, this always returns true.

Returns the least common multiple (always positive). 0.lcm(x) and x.lcm(0) return zero.

2.lcm(2)                    #=> 2
3.lcm(-7)                   #=> 21
((1<<31)-1).lcm((1<<61)-1)  #=> 4951760154835678088235319297

Returns self.

Returns true if int is an odd number.

Returns the int itself.

?a.ord    #=> 97

This method is intended for compatibility to character constant in Ruby 1.9.

For example, ?a.ord returns 97 both in 1.8 and 1.9.

Returns the Integer equal to int - 1.

1.pred      #=> 0
(-1).pred   #=> -2

Returns true if self is a prime number, else returns false.

Returns the factorization of self.

See Prime#prime_division for more details.

Returns the value as a rational. The optional argument eps is always ignored.

Rounds int to a given precision in decimal digits (default 0 digits).

Precision may be negative. Returns a floating point number when ndigits is positive, self for zero, and round down for negative.

1.round        #=> 1
1.round(2)     #=> 1.0
15.round(-1)   #=> 20

Returns the Integer equal to int + 1, same as Fixnum#next.

1.next      #=> 2
(-1).next   #=> 0

Iterates the given block int times, passing in values from zero to int - 1.

If no block is given, an Enumerator is returned instead.

5.times do |i|
  print i, " "
end
#=> 0 1 2 3 4

Casts an Integer as an OpenSSL::BN

See ‘man bn` for more info.

Convert int to a BigDecimal and return it.

require 'bigdecimal'
require 'bigdecimal/util'

42.to_d
# => #<BigDecimal:1008ef070,'0.42E2',9(36)>

As int is already an Integer, all these methods simply return the receiver.

Synonyms are to_int, floor, ceil, truncate.

An alias for to_i

Returns the value as a rational.

1.to_r        #=> (1/1)
(1<<64).to_r  #=> (18446744073709551616/1)
An alias for to_i

Iterates the given block, passing in integer values from int up to and including limit.

If no block is given, an Enumerator is returned instead.

For example:

5.upto(10) { |i| print i, " " }
#=> 5 6 7 8 9 10