type
Number
@Js
@Serializable { simple=true }
const class Number : Obj
Number represents a numeric value and an optional Unit.
constructors
fromStr |
Parse from a string according to zinc syntax |
---|---|
make |
Construct from scalar value and optional unit. |
makeDuration |
Construct from a duration, standardize unit is hours If unit is null, then a best attempt is made based on magnitude. |
makeInt |
Construct from scalar integer and optional unit. |
fields
defVal |
Default value is zero with no unit |
---|---|
nan |
Constant for not-a-number |
negInf |
Constant for negative infinity |
negOne |
Constant for -1 with no unit |
one |
Constant for 1 with no unit |
posInf |
Constant for positive infinity |
ten |
Constant for 10 with no unit |
zero |
Constant for 0 with no unit |
methods
abs |
Return absolute value of this number. |
---|---|
approx |
Return if this number is approximately equal to that - see |
clamp |
Clamp this number between the min and max. |
compare |
Compare is based on val. |
decrement |
Decrement this number. |
div |
Divide this by b. |
equals |
Equality is based on val and unit. |
hash |
Hash is based on val |
increment |
Increment this number. |
isDuration |
Does this number have a time unit which can be converted to a Fantom Duration instance. |
isInt |
Is this number a whole integer without a fractional part |
isNaN |
Is the floating value NaN. |
isNegative |
Is this a negative number |
isSpecial |
Return if this number if pos/neg infinity or NaN |
lower |
Get the ASCII lower case version of this number as a Unicode point. |
makeNum |
Construct from scalar Int, Float, or Decimal and optional unit. |
max |
Return max value. |
min |
Return min value. |
minus |
Subtract b from this. |
mod |
Return remainder of this divided by b. |
mult |
Multiple this and b. |
negate |
Negate this number. |
plus |
Add this with b. |
toCode |
Trio/zinc code representation, same as |
toDuration |
Get this number as a Fantom Duration instance |
toFloat |
Get the scalar value as an Float |
toInt |
Get the scalar value as an Int |
toLocale |
Format the number using given pattern which is an superset of |
toStr |
String representation |
unit |
Get unit associated with this number or null. |
upper |
Get the ASCII upper case version of this number as a Unicode point. |
Slot Details
abs
Number abs()
Return absolute value of this number.
approx
Bool approx(Number that, Float? tolerance := null)
Return if this number is approximately equal to that - see Float.approx
clamp
Number clamp(Number min, Number max)
Clamp this number between the min and max. If its less than min then return min, if its greater than max return max, otherwise return this number itself. The min and max must have matching units or be unitless. The result is always in the same unit as this instance.
compare
decrement
defVal
const static Number defVal := Number.<ctor>(0.0)
Default value is zero with no unit
div
equals
fromStr
hash
virtual override Int hash()
Hash is based on val
increment
isDuration
Bool isDuration()
Does this number have a time unit which can be converted to a Fantom Duration instance.
isInt
Bool isInt()
Is this number a whole integer without a fractional part
isNaN
Bool isNaN()
Is the floating value NaN.
isNegative
Bool isNegative()
Is this a negative number
isSpecial
Bool isSpecial()
Return if this number if pos/neg infinity or NaN
lower
Number lower()
Get the ASCII lower case version of this number as a Unicode point.
make
makeDuration
makeInt
makeNum
max
min
minus
mod
mult
nan
const static Number nan := Number.<ctor>(Float.nan)
Constant for not-a-number
negInf
const static Number negInf := Number.<ctor>(Float.negInf)
Constant for negative infinity
negOne
const static Number negOne := Number.<ctor>(-1.0)
Constant for -1 with no unit
negate
one
const static Number one := Number.<ctor>(1.0)
Constant for 1 with no unit
plus
posInf
const static Number posInf := Number.<ctor>(Float.posInf)
Constant for positive infinity
ten
const static Number ten := Number.<ctor>(10.0)
Constant for 10 with no unit
toCode
toDuration
toFloat
Float toFloat()
Get the scalar value as an Float
toInt
Int toInt()
Get the scalar value as an Int
toLocale
Str toLocale(Str? pattern := null)
Format the number using given pattern which is an superset of Float.toLocale
:
# optional digit 0 required digit . decimal point , grouping separator (only last one before decimal matters) U position of unit (default to suffix) pos;neg separate negative format (must specify U position)
When using the pos;neg
pattern, the "U" position must be specified in both pos and neg patterns, otherwise the unit is omitted. Note that the negative pattern always uses mimics the positive pattern for the actual digit formatting (#, 0, decimal, and grouping).
The special "B" pattern is used to format bytes; see Int.toLocale
.
If pattern is null, the following rules are used:
- If
isDuration
true, then return best fit unit is selected - If unit is non-null attempt to lookup a unit specific default pattern with the locale key "haystack::number.{unit.name}".
- If
isInt
true, then returnInt.toLocale
using sys locale default - Return
Float.toLocale
using sys locale default
Examples:
Number Pattern Result Notes ------ ------- ------- ------ 12.34 "#.####" 12.34 Optional fractional digits 12.34 "#.0000" 12.3400 Required fractional digits 12.34$ null $12.34 Haystack locale default 12$ "U 0.00" $ 12.00 Explicit unit placement -12$ "U0.##;(U#)" ($12) Alternative negative format 45% "+0.0U;-0.0U" +45% Use leading positive sign
toStr
virtual override Str toStr()
String representation
unit
Unit? unit()
Get unit associated with this number or null.
upper
Number upper()
Get the ASCII upper case version of this number as a Unicode point.
zero
const static Number zero := Number.<ctor>(0.0)
Constant for 0 with no unit