Class: BigDecimal
- Defined in:
- opal/stdlib/bigdecimal.rb,
opal/stdlib/bigdecimal.rb,
opal/stdlib/bigdecimal/util.rb,
opal/stdlib/bigdecimal/bignumber.js.rb
Overview
backtick_javascript: true
Constant Summary collapse
- VERSION =
'0'
- 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.
59 60 61 |
# File 'opal/stdlib/bigdecimal.rb', line 59 def initialize(initial, digits = 0) @bignumber = JS.new(BigNumber, initial) end |
Instance Attribute Details
#bignumber ⇒ Object (readonly)
Returns the value of attribute bignumber.
57 58 59 |
# File 'opal/stdlib/bigdecimal.rb', line 57 def bignumber @bignumber end |
Class Method Details
.limit(digits = nil) ⇒ Object
44 45 46 47 |
# File 'opal/stdlib/bigdecimal.rb', line 44 def self.limit(digits = nil) @digits = digits if digits @digits end |
.mode(mode, value = nil) ⇒ Object
49 50 51 52 53 54 55 |
# File 'opal/stdlib/bigdecimal.rb', line 49 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
16 17 18 19 |
# File 'opal/stdlib/bigdecimal.rb', line 16 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
84 85 86 87 |
# File 'opal/stdlib/bigdecimal.rb', line 84 def <(other) return false if nan? || other && other.nan? super end |
#<=(other) ⇒ Object
89 90 91 92 |
# File 'opal/stdlib/bigdecimal.rb', line 89 def <=(other) return false if nan? || other && other.nan? super end |
#<=>(other) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'opal/stdlib/bigdecimal.rb', line 74 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: ===
63 64 65 66 67 68 69 70 71 72 |
# File 'opal/stdlib/bigdecimal.rb', line 63 def ==(other) case other when self.class bignumber.JS.equals(other.bignumber) when Number bignumber.JS.equals(other) else false end end |
#>(other) ⇒ Object
94 95 96 97 |
# File 'opal/stdlib/bigdecimal.rb', line 94 def >(other) return false if nan? || other && other.nan? super end |
#>=(other) ⇒ Object
99 100 101 102 |
# File 'opal/stdlib/bigdecimal.rb', line 99 def >=(other) return false if nan? || other && other.nan? super end |
#abs ⇒ Object
104 105 106 |
# File 'opal/stdlib/bigdecimal.rb', line 104 def abs self.class.new(bignumber.JS.abs) end |
#add(other, digits = 0) ⇒ Object Also known as: +
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'opal/stdlib/bigdecimal.rb', line 108 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
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'opal/stdlib/bigdecimal.rb', line 128 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
142 143 144 145 146 147 148 149 150 151 |
# File 'opal/stdlib/bigdecimal.rb', line 142 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
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'opal/stdlib/bigdecimal.rb', line 153 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
177 178 179 |
# File 'opal/stdlib/bigdecimal.rb', line 177 def finite? bignumber.JS.isFinite end |
#infinite? ⇒ Boolean
181 182 183 184 |
# File 'opal/stdlib/bigdecimal.rb', line 181 def infinite? return nil if finite? || nan? bignumber.JS.isNegative ? -1 : 1 end |
#minus(other) ⇒ Object Also known as: -
186 187 188 189 |
# File 'opal/stdlib/bigdecimal.rb', line 186 def minus(other) other, _ = coerce(other) self.class.new(bignumber.JS.minus(other.bignumber)) end |
#mult(other, digits = nil) ⇒ Object Also known as: *
191 192 193 194 195 196 197 198 199 |
# File 'opal/stdlib/bigdecimal.rb', line 191 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
201 202 203 |
# File 'opal/stdlib/bigdecimal.rb', line 201 def nan? bignumber.JS.isNaN end |
#quo(other) ⇒ Object Also known as: /
205 206 207 208 |
# File 'opal/stdlib/bigdecimal.rb', line 205 def quo(other) other, _ = coerce(other) self.class.new(bignumber.JS.dividedBy(other.bignumber)) end |
#sign ⇒ Object
210 211 212 213 214 215 216 217 |
# File 'opal/stdlib/bigdecimal.rb', line 210 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
219 220 221 222 |
# File 'opal/stdlib/bigdecimal.rb', line 219 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
107 108 109 |
# File 'opal/stdlib/bigdecimal/util.rb', line 107 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"
87 88 89 90 91 92 93 94 95 |
# File 'opal/stdlib/bigdecimal/util.rb', line 87 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
224 225 226 |
# File 'opal/stdlib/bigdecimal.rb', line 224 def to_f bignumber.JS.toNumber end |
#to_s(s = '') ⇒ Object Also known as: inspect
228 229 230 |
# File 'opal/stdlib/bigdecimal.rb', line 228 def to_s(s = '') bignumber.JS.toString end |
#zero? ⇒ Boolean
232 233 234 |
# File 'opal/stdlib/bigdecimal.rb', line 232 def zero? bignumber.JS.isZero end |