Class: BSON::Decimal128

Inherits:
Object
  • Object
show all
Includes:
JSON
Defined in:
lib/bson/decimal128.rb,
lib/bson/decimal128/builder.rb

Defined Under Namespace

Modules: Builder Classes: InvalidArgument, InvalidRange, InvalidString

Constant Summary collapse

BSON_TYPE =

A Decimal128 is type 0x13 in the BSON spec.

Since:

  • 4.2.0

19.chr.force_encoding(BINARY).freeze
EXPONENT_OFFSET =

Exponent offset.

Since:

  • 4.2.0

6176.freeze
MIN_EXPONENT =

Minimum exponent.

Since:

  • 4.2.0

-6176.freeze
MAX_EXPONENT =

Maximum exponent.

Since:

  • 4.2.0

6111.freeze
MAX_DIGITS_OF_PRECISION =

Maximum digits of precision.

Since:

  • 4.2.0

34.freeze
EXTENDED_JSON_KEY =

Key for this type when converted to extended json.

Since:

  • 4.2.0

"$numberDecimal".freeze
NATIVE_TYPE =

The native type to which this object can be converted.

Since:

  • 4.2.0

BigDecimal

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JSON

#to_json

Constructor Details

#initialize(object) ⇒ Decimal128

Create a new Decimal128 from a BigDecimal.

Examples:

Create a Decimal128 from a BigDecimal.

Decimal128.new(big_decimal)

Parameters:

  • object (String, BigDecimal)

    The BigDecimal or String to use for instantiating a Decimal128.

Raises:

  • (InvalidBigDecimal)

    Raise error unless object argument is a BigDecimal.

Since:

  • 4.2.0


98
99
100
101
102
103
104
105
106
# File 'lib/bson/decimal128.rb', line 98

def initialize(object)
  if object.is_a?(String)
    set_bits(*Builder::FromString.new(object).bits)
  elsif object.is_a?(BigDecimal)
    set_bits(*Builder::FromBigDecimal.new(object).bits)
  else
    raise InvalidArgument.new
  end
end

Class Method Details

.from_bits(low, high) ⇒ BSON::Decimal128

Instantiate a Decimal128 from high and low bits.

Examples:

Create a Decimal128 from high and low bits.

BSON::Decimal128.from_bits(high, low)

Parameters:

  • high (Integer)

    The high order bits.

  • low (Integer)

    The low order bits.

Returns:

Since:

  • 4.2.0


234
235
236
237
238
# File 'lib/bson/decimal128.rb', line 234

def from_bits(low, high)
  decimal = allocate
  decimal.send(:set_bits, low, high)
  decimal
end

.from_bson(buffer) ⇒ BSON::Decimal128

Deserialize the decimal128 from raw BSON bytes.

Examples:

Get the decimal128 from BSON.

Decimal128.from_bson(bson)

Parameters:

  • buffer (ByteBuffer)

    The byte buffer.

Returns:

Since:

  • 4.2.0


203
204
205
# File 'lib/bson/decimal128.rb', line 203

def from_bson(buffer)
  from_bits(*buffer.get_decimal128_bytes.unpack('Q<*'))
end

.from_string(string) ⇒ BSON::Decimal128

Instantiate a Decimal128 from a string.

Examples:

Create a Decimal128 from a string.

BSON::Decimal128.from_string("1.05E+3")

Parameters:

  • string (String)

    The string to parse.

Returns:

Raises:

Since:

  • 4.2.0


219
220
221
# File 'lib/bson/decimal128.rb', line 219

def from_string(string)
  from_bits(*Builder::FromString.new(string).bits)
end

Instance Method Details

#==(other) ⇒ true, false Also known as: eql?

Check equality of the decimal128 object with another object.

Examples:

Check if the decimal128 object is equal to the other.

decimal == other

Parameters:

  • other (Object)

    The object to check against.

Returns:

  • (true, false)

    If the objects are equal.

Since:

  • 4.2.0


80
81
82
83
84
# File 'lib/bson/decimal128.rb', line 80

def ==(other)
  return false unless other.is_a?(Decimal128)
  @high == other.instance_variable_get(:@high) &&
      @low == other.instance_variable_get(:@low)
end

#as_json(*args) ⇒ Hash

Get the Decimal128 as JSON hash data.

Examples:

Get the Decimal128 as a JSON hash.

decimal.as_json

Returns:

  • (Hash)

    The number as a JSON hash.

Since:

  • 4.2.0


66
67
68
# File 'lib/bson/decimal128.rb', line 66

def as_json(*args)
  { EXTENDED_JSON_KEY => to_s }
end

#hashInteger

Get the hash value for the decimal128.

Examples:

Get the hash value.

decimal.hash

Returns:

Since:

  • 4.2.0


130
131
132
133
134
# File 'lib/bson/decimal128.rb', line 130

def hash
  num = @high << 64
  num |= @low
  num.hash
end

#inspectString

Get a nice string for use with object inspection.

Examples:

Inspect the decimal128 object.

decimal128.inspect

Returns:

  • (String)

    The decimal as a string.

Since:

  • 4.2.0


144
145
146
# File 'lib/bson/decimal128.rb', line 144

def inspect
  "BSON::Decimal128('#{to_s}')"
end

#to_big_decimalBigDecimal

Get a Ruby BigDecimal object corresponding to this Decimal128. Note that, when converting to a Ruby BigDecimal, non-zero significant digits are preserved but trailing zeroes may be lost. See the following example:

Note that the the BSON::Decimal128 object can represent -NaN, sNaN, and -sNaN while Ruby's BigDecimal cannot.

Examples:

decimal128 = BSON::Decimal128.new("0.200")
  => BSON::Decimal128('0.200')
big_decimal = decimal128.to_big_decimal
  => #<BigDecimal:7fc619c95388,'0.2E0',9(18)>
big_decimal.to_s
  => "0.2E0"

Returns:

  • (BigDecimal)

    The decimal as a BigDecimal.

Since:

  • 4.2.0


180
181
182
# File 'lib/bson/decimal128.rb', line 180

def to_big_decimal
  @big_decimal ||= BigDecimal.new(to_s)
end

#to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?) ⇒ BSON::ByteBuffer

Get the decimal128 as its raw BSON data.

Examples:

Get the raw bson bytes in a buffer.

decimal.to_bson

Returns:

  • (BSON::ByteBuffer)

    The buffer with the encoded object.

See Also:

Since:

  • 4.2.0


118
119
120
# File 'lib/bson/decimal128.rb', line 118

def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
  buffer.put_decimal128(@low, @high)
end

#to_sString Also known as: to_str

Get the string representation of the decimal128.

Examples:

Get the decimal128 as a string.

decimal128.to_s

Returns:

  • (String)

    The decimal128 as a string.

Since:

  • 4.2.0


156
157
158
# File 'lib/bson/decimal128.rb', line 156

def to_s
  @string ||= Builder::ToString.new(self).string
end