class BSON::Decimal128::Builder::ToString
Helper class for getting a String
representation of a Decimal128
object.
@api private
@since 4.2.0
Constants
Public Class Methods
Source
# File lib/bson/decimal128/builder.rb, line 363 def initialize(decimal128) @decimal128 = decimal128 end
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
Public Instance Methods
Source
# File lib/bson/decimal128/builder.rb, line 377 def string return NAN_STRING if nan? str = infinity? ? INFINITY_STRING : create_string negative? ? "-#{str}" : str end
Get the string representing the Decimal128
object.
@example Get a string representing the decimal128.
builder.string
@return [ String
] The string representing the decimal128 object.
@note The returned string may be frozen.
@since 4.2.0
Private Instance Methods
Source
# File lib/bson/decimal128/builder.rb, line 423 def bits_to_significand significand = high_bits & 0x1ffffffffffff significand = significand << 64 significand |= low_bits end
Source
# File lib/bson/decimal128/builder.rb, line 385 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
Source
# File lib/bson/decimal128/builder.rb, line 413 def exponent @exponent ||= two_highest_bits_set? ? ((high_bits & 0x1fffe00000000000) >> 47) - Decimal128::EXPONENT_OFFSET : ((high_bits & 0x7fff800000000000) >> 49) - Decimal128::EXPONENT_OFFSET end
Source
# File lib/bson/decimal128/builder.rb, line 445 def high_bits @decimal128.instance_variable_get(:@high) end
Source
# File lib/bson/decimal128/builder.rb, line 441 def infinity? high_bits & INFINITY_MASK == INFINITY_MASK end
Source
# File lib/bson/decimal128/builder.rb, line 449 def low_bits @decimal128.instance_variable_get(:@low) end
Source
# File lib/bson/decimal128/builder.rb, line 433 def nan? high_bits & NAN_MASK == NAN_MASK end
Source
# File lib/bson/decimal128/builder.rb, line 437 def negative? high_bits & SIGN_BIT_MASK == SIGN_BIT_MASK end
Source
# File lib/bson/decimal128/builder.rb, line 405 def scientific_exponent @scientific_exponent ||= (significand.length - 1) + exponent end
Source
# File lib/bson/decimal128/builder.rb, line 419 def significand @significand ||= two_highest_bits_set? ? '0' : bits_to_significand.to_s end
Source
# File lib/bson/decimal128/builder.rb, line 429 def two_highest_bits_set? high_bits & TWO_HIGHEST_BITS_SET == TWO_HIGHEST_BITS_SET end
Source
# File lib/bson/decimal128/builder.rb, line 409 def use_scientific_notation? exponent > 0 || scientific_exponent < -6 end