Class: BigDecimal
- Defined in:
- opal/stdlib/bigdecimal.rb,
opal/stdlib/bigdecimal.rb,
opal/stdlib/bigdecimal/bignumber.js.rb
Constant Summary
- 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_f ⇒ Object
- #to_s(s = '') ⇒ Object
- #zero? ⇒ Boolean
Methods inherited from Numeric
Constructor Details
#initialize(initial, digits = 0) ⇒ BigDecimal
Returns a new instance of BigDecimal
43 44 45 |
# File 'opal/stdlib/bigdecimal.rb', line 43 def initialize(initial, digits = 0) @bignumber = JS.new(BigNumber, initial) end |
Instance Attribute Details
#bignumber ⇒ Object (readonly)
Returns the value of attribute bignumber
41 42 43 |
# File 'opal/stdlib/bigdecimal.rb', line 41 def bignumber @bignumber end |
Class Method Details
.limit(digits = nil) ⇒ Object
28 29 30 31 |
# File 'opal/stdlib/bigdecimal.rb', line 28 def self.limit(digits = nil) @digits = digits if digits @digits end |
.mode(mode, value = nil) ⇒ Object
33 34 35 36 37 38 39 |
# File 'opal/stdlib/bigdecimal.rb', line 33 def self.mode(mode, value = nil) case mode when ROUND_MODE @round_mode = value if value @round_mode || ROUND_HALF_UP end end |
Instance Method Details
#<(other) ⇒ Object
72 73 74 75 |
# File 'opal/stdlib/bigdecimal.rb', line 72 def <(other) return false if nan? || other && other.nan? super end |
#<=(other) ⇒ Object
77 78 79 80 |
# File 'opal/stdlib/bigdecimal.rb', line 77 def <=(other) return false if nan? || other && other.nan? super end |
#<=>(other) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'opal/stdlib/bigdecimal.rb', line 60 def <=>(other) case other when self.class result = bignumber.JS.comparedTo(other.bignumber) when Number result = bignumber.JS.comparedTo(other) else result = nil end `#{result} === null ? nil : #{result}` end |
#==(other) ⇒ Object Also known as: ===
47 48 49 50 51 52 53 54 55 56 |
# File 'opal/stdlib/bigdecimal.rb', line 47 def ==(other) case other when self.class bignumber.JS.equals(other.bignumber) when Number bignumber.JS.equals(other) else false end end |
#>(other) ⇒ Object
82 83 84 85 |
# File 'opal/stdlib/bigdecimal.rb', line 82 def >(other) return false if nan? || other && other.nan? super end |
#>=(other) ⇒ Object
87 88 89 90 |
# File 'opal/stdlib/bigdecimal.rb', line 87 def >=(other) return false if nan? || other && other.nan? super end |
#abs ⇒ Object
92 93 94 |
# File 'opal/stdlib/bigdecimal.rb', line 92 def abs self.class.new(bignumber.JS.abs) end |
#add(other, digits = 0) ⇒ Object Also known as: +
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'opal/stdlib/bigdecimal.rb', line 96 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
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'opal/stdlib/bigdecimal.rb', line 118 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
132 133 134 135 136 137 138 139 140 141 |
# File 'opal/stdlib/bigdecimal.rb', line 132 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
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'opal/stdlib/bigdecimal.rb', line 143 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
167 168 169 |
# File 'opal/stdlib/bigdecimal.rb', line 167 def finite? bignumber.JS.isFinite end |
#infinite? ⇒ Boolean
171 172 173 174 |
# File 'opal/stdlib/bigdecimal.rb', line 171 def infinite? return nil if finite? || nan? bignumber.JS.isNegative ? -1 : 1 end |
#minus(other) ⇒ Object Also known as: -
176 177 178 179 |
# File 'opal/stdlib/bigdecimal.rb', line 176 def minus(other) other, _ = coerce(other) self.class.new(bignumber.JS.minus(other.bignumber)) end |
#mult(other, digits = nil) ⇒ Object Also known as: *
183 184 185 186 187 188 189 190 191 |
# File 'opal/stdlib/bigdecimal.rb', line 183 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
195 196 197 |
# File 'opal/stdlib/bigdecimal.rb', line 195 def nan? bignumber.JS.isNaN end |
#quo(other) ⇒ Object Also known as: /
199 200 201 202 |
# File 'opal/stdlib/bigdecimal.rb', line 199 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_f ⇒ Object
220 221 222 |
# File 'opal/stdlib/bigdecimal.rb', line 220 def to_f bignumber.JS.toNumber end |
#to_s(s = '') ⇒ Object
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 |