class BSON::Decimal128::Builder::ToString

@since 4.2.0
@api private
Helper class for getting a String representation of a Decimal128 object.

def bits_to_significand

def bits_to_significand
  significand = high_bits & 0x1ffffffffffff
  significand = significand << 64
  significand |= low_bits
end

def create_string

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

def exponent

def exponent
  @exponent ||= two_highest_bits_set? ?
      ((high_bits & 0x1fffe00000000000) >> 47) - Decimal128::EXPONENT_OFFSET :
      ((high_bits & 0x7fff800000000000) >> 49) - Decimal128::EXPONENT_OFFSET
end

def high_bits

def high_bits
  @decimal128.instance_variable_get(:@high)
end

def infinity?

def infinity?
  high_bits & INFINITY_MASK == INFINITY_MASK
end

def initialize(decimal128)

Other tags:
    Since: - 4.2.0

Parameters:
  • decimal128 (Decimal128) -- The decimal128 object to

Other tags:
    Example: Create the ToString builder. -
def initialize(decimal128)
  @decimal128 = decimal128
end

def low_bits

def low_bits
  @decimal128.instance_variable_get(:@low)
end

def nan?

def nan?
  high_bits & NAN_MASK == NAN_MASK
end

def negative?

def negative?
  high_bits & SIGN_BIT_MASK == SIGN_BIT_MASK
end

def scientific_exponent

def scientific_exponent
  @scientific_exponent ||= (significand.length - 1) + exponent
end

def significand

def significand
  @significand ||= two_highest_bits_set? ? '0' : bits_to_significand.to_s
end

def string

Other tags:
    Since: - 4.2.0

Other tags:
    Note: - The returned string may be frozen.

Returns:
  • (String) - The string representing the decimal128 object.

Other tags:
    Example: Get a string representing the decimal128. -
def string
  return NAN_STRING if nan?
  str = infinity? ? INFINITY_STRING : create_string
  negative? ? "-#{str}" : str
end

def two_highest_bits_set?

def two_highest_bits_set?
  high_bits & TWO_HIGHEST_BITS_SET == TWO_HIGHEST_BITS_SET
end

def use_scientific_notation?

def use_scientific_notation?
  exponent > 0 || scientific_exponent < -6
end