class BSON::Decimal128::Builder::ToString

Helper class for getting a String representation of a Decimal128 object.

@api private

@since 4.2.0

Constants

INFINITY_STRING

String representing an Infinity value.

@return [ String ] The string representing Infinity.

@since 4.2.0

NAN_STRING

String representing a NaN value.

@return [ String ] The string representing NaN.

@since 4.2.0

Public Class Methods

new(decimal128) click to toggle source

Initialize the FromBigDecimal Builder object.

@example Create the ToString builder.

Builder::ToString.new(big_decimal)

@param [ Decimal128 ] decimal128 The decimal128 object to

create a String from.

@since 4.2.0

# File lib/bson/decimal128/builder.rb, line 358
def initialize(decimal128)
  @decimal128 = decimal128
end

Public Instance Methods

string() click to toggle source

Get the string representing the Decimal128 object.

@example Get a string representing the decimal128.

builder.string

@return [ String ] The string representing the decimal128 object.

@since 4.2.0

# File lib/bson/decimal128/builder.rb, line 370
def string
  return NAN_STRING if nan?
  str = infinity? ? INFINITY_STRING : create_string
  negative? ? '-' << str : str
end

Private Instance Methods

bits_to_significand() click to toggle source
# File lib/bson/decimal128/builder.rb, line 416
def bits_to_significand
  significand = high_bits & 0x1ffffffffffff
  significand = significand << 64
  significand |= low_bits
end
create_string() click to toggle source
# File lib/bson/decimal128/builder.rb, line 378
def create_string
  if use_scientific_notation?
    exp_pos_sign = exponent < 0 ? '' : '+'
    if significand.length > 1
      str = "#{significand[0]}.#{significand[1..-1]}E#{exp_pos_sign}#{scientific_exponent}"
    else
      str = "#{significand}E#{exp_pos_sign}#{scientific_exponent}"
    end
  elsif exponent < 0
    if significand.length > exponent.abs
      decimal_point_index = significand.length - exponent.abs
      str = "#{significand[0..decimal_point_index-1]}.#{significand[decimal_point_index..-1]}"
    else
      left_zero_pad = (exponent + significand.length).abs
      str = "0.#{'0' * left_zero_pad}#{significand}"
    end
  end
  str || significand
end
exponent() click to toggle source
# File lib/bson/decimal128/builder.rb, line 406
def exponent
  @exponent ||= two_highest_bits_set? ?
      ((high_bits & 0x1fffe00000000000) >> 47) - Decimal128::EXPONENT_OFFSET :
      ((high_bits & 0x7fff800000000000) >> 49) - Decimal128::EXPONENT_OFFSET
end
high_bits() click to toggle source
# File lib/bson/decimal128/builder.rb, line 438
def high_bits
  @decimal128.instance_variable_get(:@high)
end
infinity?() click to toggle source
# File lib/bson/decimal128/builder.rb, line 434
def infinity?
  high_bits & INFINITY_MASK == INFINITY_MASK
end
low_bits() click to toggle source
# File lib/bson/decimal128/builder.rb, line 442
def low_bits
  @decimal128.instance_variable_get(:@low)
end
nan?() click to toggle source
# File lib/bson/decimal128/builder.rb, line 426
def nan?
  high_bits & NAN_MASK == NAN_MASK
end
negative?() click to toggle source
# File lib/bson/decimal128/builder.rb, line 430
def negative?
  high_bits & SIGN_BIT_MASK == SIGN_BIT_MASK
end
scientific_exponent() click to toggle source
# File lib/bson/decimal128/builder.rb, line 398
def scientific_exponent
  @scientific_exponent ||= (significand.length - 1) + exponent
end
significand() click to toggle source
# File lib/bson/decimal128/builder.rb, line 412
def significand
  @significand ||= two_highest_bits_set? ? '0' : bits_to_significand.to_s
end
two_highest_bits_set?() click to toggle source
# File lib/bson/decimal128/builder.rb, line 422
def two_highest_bits_set?
  high_bits & TWO_HIGHEST_BITS_SET == TWO_HIGHEST_BITS_SET
end
use_scientific_notation?() click to toggle source
# File lib/bson/decimal128/builder.rb, line 402
def use_scientific_notation?
  exponent > 0 || scientific_exponent < -6
end