- Index
- »
- fan.haystack
- »
- Number
Number
@Js
@Serializable { simple=true }
const class Number : Obj
Number represents a numeric value and an optional Unit.
Construct from scalar integer and optional unit
Parse from a string according to zinc syntax
Construct from a duration, standardize unit is hours If unit is null, then a best attempt is made based on magnitude
Construct from scalar value and optional unit
Constant for 0 with no unit
Constant for -1 with no unit
Constant for negative infinity
Constant for 1 with no unit
Constant for not-a-number
Constant for 10 with no unit
Constant for positive infinity
Default value is zero with no unit
Subtract b from this
Compare is based on val
Return remainder of this divided by b
Multiple this and b
Get the ASCII upper case version of this number as a Unicode point
Increment this number
Is the floating value NaN
Divide this by b
Return min value
Return if this number if pos/neg infinity or NaN
Is this number a whole integer without a fractional part
Construct from scalar Int, Float, or Decimal and optional unit
Clamp this number between the min and max
String representation
Is this a negative number
Get the scalar value as an Int
Format the number using given pattern which is an superset of Float.toLocale
Does this number have a time unit which can be converted to a Fantom Duration instance
Return max value
Trio/zinc code representation, same as toStr
Get the ASCII lower case version of this number as a Unicode point
Get the scalar value as an Float
Add this with b
Get unit associated with this number or null
Return absolute value of this number
Decrement this number
Negate this number
Equality is based on val and unit
Get this number as a Fantom Duration instance
Hash is based on val
Return if this number is approximately equal to that - see Float.approx
Number abs()
Return absolute value of this number.
Bool approx(Number that, Float? tolerance)
Return if this number is approximately equal to that - see Float.approx
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.
virtual Int compare(Obj that)
Compare is based on val. Throw UnitErr is this and b have incompatible units.
@Operator
Number decrement()
Decrement this number. Shortcut is --a.
const static Number : defVal
Default value is zero with no unit
@Operator
Number div(Number b)
Divide this by b. Shortcut is a/b. The resulting unit is derived from the quotient of this and b. Throw UnitErr if a/b does not match a unit in the unit database.
virtual Bool equals(Obj? that)
Equality is based on val and unit. NaN is equal to itself (like Float.compare, but unlike Float.equals)
static new fromStr(Str s, Bool checked)
Parse from a string according to zinc syntax
virtual Int hash()
Hash is based on val
@Operator
Number increment()
Increment this number. Shortcut is ++a.
Bool isDuration()
Does this number have a time unit which can be converted to a Fantom Duration instance.
Bool isInt()
Is this number a whole integer without a fractional part
Bool isNaN()
Is the floating value NaN.
Bool isNegative()
Is this a negative number
Bool isSpecial()
Return if this number if pos/neg infinity or NaN
Number lower()
Get the ASCII lower case version of this number as a Unicode point.
new make(Float val, Unit? unit)
Construct from scalar value and optional unit.
new makeDuration(Duration dur, Unit? unit)
Construct from a duration, standardize unit is hours If unit is null, then a best attempt is made based on magnitude.
static new makeInt(Int val, Unit? unit)
Construct from scalar integer and optional unit.
static Number makeNum(Num val, Unit? unit)
Construct from scalar Int, Float, or Decimal and optional unit.
Number max(Number that)
Return max value. Units are not checked for this comparison.
Number min(Number that)
Return min value. Units are not checked for this comparison.
@Operator
Number minus(Number b)
Subtract b from this. Shortcut is a-b. The b.unit must match this.unit.
@Operator
Number mod(Number b)
Return remainder of this divided by b. Shortcut is a%b. The unit of b must be null.
@Operator
Number mult(Number b)
Multiple this and b. Shortcut is a*b. The resulting unit is derived from the product of this and b. Throw UnitErr if a*b does not match a unit in the unit database.
const static Number : nan
Constant for not-a-number
const static Number : negInf
Constant for negative infinity
const static Number : negOne
Constant for -1 with no unit
@Operator
Number negate()
Negate this number. Shortcut is -a.
const static Number : one
Constant for 1 with no unit
@Operator
Number plus(Number b)
Add this with b. Shortcut is a+b. Throw UnitErr is this and b have incompatible units.
const static Number : posInf
Constant for positive infinity
const static Number : ten
Constant for 10 with no unit
Str toCode()
Trio/zinc code representation, same as toStr
Duration? toDuration(Bool checked)
Get this number as a Fantom Duration instance
Float toFloat()
Get the scalar value as an Float
Int toInt()
Get the scalar value as an Int
Str toLocale(Str? pattern)
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 return Int.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
virtual Str toStr()
String representation
Unit? unit()
Get unit associated with this number or null.
Number upper()
Get the ASCII upper case version of this number as a Unicode point.
const static Number : zero
Constant for 0 with no unit