Class: BigDecimal
- Defined in:
- opal/stdlib/bigdecimal.rb,
 opal/stdlib/bigdecimal.rb,
 opal/stdlib/bigdecimal/util.rb,
 opal/stdlib/bigdecimal/bignumber.js.rb
Constant Summary collapse
- ROUND_MODE =
- 256
- ROUND_UP =
          NOTE: the numeric values of the ROUND_* constants follow BigNumber.js, they are NOT the same as MRI 
- 0
- ROUND_DOWN =
- 1
- ROUND_CEILING =
- 2
- ROUND_FLOOR =
- 3
- ROUND_HALF_UP =
- 4
- ROUND_HALF_DOWN =
- 5
- ROUND_HALF_EVEN =
- 6
- SIGN_NaN =
- 0
- SIGN_POSITIVE_ZERO =
- 1
- SIGN_NEGATIVE_ZERO =
- -1 
- SIGN_POSITIVE_FINITE =
- 2
- SIGN_NEGATIVE_FINITE =
- -2 
- SIGN_POSITIVE_INFINITE =
- 3
- SIGN_NEGATIVE_INFINITE =
- -3 
Instance Attribute Summary collapse
- 
  
    
      #bignumber  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute bignumber. 
Class Method Summary collapse
Instance Method Summary collapse
- #<(other) ⇒ Object
- #<=(other) ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object (also: #===)
- #>(other) ⇒ Object
- #>=(other) ⇒ Object
- #abs ⇒ Object
- #add(other, digits = 0) ⇒ Object (also: #+)
- #ceil(n = nil) ⇒ Object
- #coerce(other) ⇒ Object
- #div(other, digits = nil) ⇒ Object
- #finite? ⇒ Boolean
- #infinite? ⇒ Boolean
- 
  
    
      #initialize(initial, digits = 0)  ⇒ BigDecimal 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    A new instance of BigDecimal. 
- #minus(other) ⇒ Object (also: #-)
- #mult(other, digits = nil) ⇒ Object (also: #*)
- #nan? ⇒ Boolean
- #quo(other) ⇒ Object (also: #/)
- #sign ⇒ Object
- #sub(other, precision) ⇒ Object
- 
  
    
      #to_d  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    call-seq: a.to_d -> bigdecimal. 
- 
  
    
      #to_digits  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    call-seq: a.to_digits -> string. 
- #to_f ⇒ Object
- #to_s(s = '') ⇒ Object (also: #inspect)
- #zero? ⇒ Boolean
Methods inherited from Numeric
Constructor Details
#initialize(initial, digits = 0) ⇒ BigDecimal
Returns a new instance of BigDecimal.
| 55 56 57 | # File 'opal/stdlib/bigdecimal.rb', line 55 def initialize(initial, digits = 0) @bignumber = JS.new(BigNumber, initial) end | 
Instance Attribute Details
#bignumber ⇒ Object (readonly)
Returns the value of attribute bignumber.
| 53 54 55 | # File 'opal/stdlib/bigdecimal.rb', line 53 def bignumber @bignumber end | 
Class Method Details
.limit(digits = nil) ⇒ Object
| 40 41 42 43 | # File 'opal/stdlib/bigdecimal.rb', line 40 def self.limit(digits = nil) @digits = digits if digits @digits end | 
.mode(mode, value = nil) ⇒ Object
| 45 46 47 48 49 50 51 | # File 'opal/stdlib/bigdecimal.rb', line 45 def self.mode(mode, value = nil) case mode when ROUND_MODE @round_mode = value if value @round_mode || ROUND_HALF_UP end end | 
.new(*args, **kwargs) ⇒ Object
| 14 15 16 17 | # File 'opal/stdlib/bigdecimal.rb', line 14 def BigDecimal.new(*args, **kwargs) warn 'BigDecimal.new is deprecated; use BigDecimal() method instead.', uplevel: 1 BigDecimal(*args, **kwargs) end | 
Instance Method Details
#<(other) ⇒ Object
| 80 81 82 83 | # File 'opal/stdlib/bigdecimal.rb', line 80 def <(other) return false if nan? || other && other.nan? super end | 
#<=(other) ⇒ Object
| 85 86 87 88 | # File 'opal/stdlib/bigdecimal.rb', line 85 def <=(other) return false if nan? || other && other.nan? super end | 
#<=>(other) ⇒ Object
| 70 71 72 73 74 75 76 77 78 | # File 'opal/stdlib/bigdecimal.rb', line 70 def <=>(other) result = case other when self.class bignumber.JS.comparedTo(other.bignumber) when Number bignumber.JS.comparedTo(other) end `#{result} === null ? nil : #{result}` end | 
#==(other) ⇒ Object Also known as: ===
| 59 60 61 62 63 64 65 66 67 68 | # File 'opal/stdlib/bigdecimal.rb', line 59 def ==(other) case other when self.class bignumber.JS.equals(other.bignumber) when Number bignumber.JS.equals(other) else false end end | 
#>(other) ⇒ Object
| 90 91 92 93 | # File 'opal/stdlib/bigdecimal.rb', line 90 def >(other) return false if nan? || other && other.nan? super end | 
#>=(other) ⇒ Object
| 95 96 97 98 | # File 'opal/stdlib/bigdecimal.rb', line 95 def >=(other) return false if nan? || other && other.nan? super end | 
#abs ⇒ Object
| 100 101 102 | # File 'opal/stdlib/bigdecimal.rb', line 100 def abs self.class.new(bignumber.JS.abs) end | 
#add(other, digits = 0) ⇒ Object Also known as: +
| 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | # File 'opal/stdlib/bigdecimal.rb', line 104 def add(other, digits = 0) if digits.nil? raise TypeError, 'wrong argument type nil (expected Fixnum)' end if digits < 0 raise ArgumentError, 'argument must be positive' end other, _ = coerce(other) result = bignumber.JS.plus(other.bignumber) if digits > 0 result = result.JS.toDigits(digits, self.class.mode(ROUND_MODE)) end self.class.new(result) end | 
#ceil(n = nil) ⇒ Object
| 124 125 126 127 128 129 130 131 132 133 134 135 136 | # File 'opal/stdlib/bigdecimal.rb', line 124 def ceil(n = nil) unless bignumber.JS.isFinite raise FloatDomainError, "Computation results to 'Infinity'" end if n.nil? bignumber.JS.round(0, ROUND_CEILING).JS.toNumber elsif n >= 0 self.class.new(bignumber.JS.round(n, ROUND_CEILING)) else self.class.new(bignumber.JS.round(0, ROUND_CEILING)) end end | 
#coerce(other) ⇒ Object
| 138 139 140 141 142 143 144 145 146 147 | # File 'opal/stdlib/bigdecimal.rb', line 138 def coerce(other) case other when self.class [other, self] when Number [self.class.new(other), self] else raise TypeError, "#{other.class} can't be coerced into #{self.class}" end end | 
#div(other, digits = nil) ⇒ Object
| 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | # File 'opal/stdlib/bigdecimal.rb', line 149 def div(other, digits = nil) return self / other if digits == 0 other, _ = coerce(other) if nan? || other.nan? raise FloatDomainError, "Computation results to 'NaN'(Not a Number)" end if digits.nil? if other.zero? raise ZeroDivisionError, 'divided by 0' end if infinite? raise FloatDomainError, "Computation results to 'Infinity'" end return self.class.new(bignumber.JS.dividedToIntegerBy(other.bignumber)) end self.class.new(bignumber.JS.dividedBy(other.bignumber).JS.round(digits, self.class.mode(ROUND_MODE))) end | 
#finite? ⇒ Boolean
| 173 174 175 | # File 'opal/stdlib/bigdecimal.rb', line 173 def finite? bignumber.JS.isFinite end | 
#infinite? ⇒ Boolean
| 177 178 179 180 | # File 'opal/stdlib/bigdecimal.rb', line 177 def infinite? return nil if finite? || nan? bignumber.JS.isNegative ? -1 : 1 end | 
#minus(other) ⇒ Object Also known as: -
| 182 183 184 185 | # File 'opal/stdlib/bigdecimal.rb', line 182 def minus(other) other, _ = coerce(other) self.class.new(bignumber.JS.minus(other.bignumber)) end | 
#mult(other, digits = nil) ⇒ Object Also known as: *
| 187 188 189 190 191 192 193 194 195 | # File 'opal/stdlib/bigdecimal.rb', line 187 def mult(other, digits = nil) other, _ = coerce(other) if digits.nil? return self.class.new(bignumber.JS.times(other.bignumber)) end self.class.new(bignumber.JS.times(other.bignumber).JS.round(digits, self.class.mode(ROUND_MODE))) end | 
#nan? ⇒ Boolean
| 197 198 199 | # File 'opal/stdlib/bigdecimal.rb', line 197 def nan? bignumber.JS.isNaN end | 
#quo(other) ⇒ Object Also known as: /
| 201 202 203 204 | # File 'opal/stdlib/bigdecimal.rb', line 201 def quo(other) other, _ = coerce(other) self.class.new(bignumber.JS.dividedBy(other.bignumber)) end | 
#sign ⇒ Object
| 206 207 208 209 210 211 212 213 | # File 'opal/stdlib/bigdecimal.rb', line 206 def sign if bignumber.JS.isNaN return SIGN_NaN end if bignumber.JS.isZero return bignumber.JS.isNegative ? SIGN_NEGATIVE_ZERO : SIGN_POSITIVE_ZERO end end | 
#sub(other, precision) ⇒ Object
| 215 216 217 218 | # File 'opal/stdlib/bigdecimal.rb', line 215 def sub(other, precision) other, _ = coerce(other) self.class.new(bignumber.JS.minus(other.bignumber)) end | 
#to_d ⇒ Object
call-seq: a.to_d -> bigdecimal
Returns self.
require 'bigdecimal/util'
d = BigDecimal("3.14")
d.to_d                       # => 0.314e1
| 106 107 108 | # File 'opal/stdlib/bigdecimal/util.rb', line 106 def to_d self end | 
#to_digits ⇒ Object
call-seq: a.to_digits -> string
Converts a BigDecimal to a String of the form "nnnnnn.mmm". This method is deprecated; use BigDecimal#to_s("F") instead.
require 'bigdecimal/util'
d = BigDecimal("3.14")
d.to_digits                  # => "3.14"
| 86 87 88 89 90 91 92 93 94 | # File 'opal/stdlib/bigdecimal/util.rb', line 86 def to_digits if nan? || infinite? || zero? to_s else i = to_i.to_s _, f, _, z = frac.split i + '.' + ('0' * -z) + f end end | 
#to_f ⇒ Object
| 220 221 222 | # File 'opal/stdlib/bigdecimal.rb', line 220 def to_f bignumber.JS.toNumber end | 
#to_s(s = '') ⇒ Object Also known as: inspect
| 224 225 226 | # File 'opal/stdlib/bigdecimal.rb', line 224 def to_s(s = '') bignumber.JS.toString end | 
#zero? ⇒ Boolean
| 228 229 230 | # File 'opal/stdlib/bigdecimal.rb', line 228 def zero? bignumber.JS.isZero end |