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 Float.approx

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 toStr

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 Float.toLocale

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

virtual override Int compare(Obj that)

Compare is based on val. Throw UnitErr is this and b have incompatible units.

decrement

@Operator
Number decrement()

Decrement this number. Shortcut is --a.

defVal

const static Number defVal := Number.<ctor>(0.0)

Default value is zero with no unit

div

@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.

equals

virtual override Bool equals(Obj? that)

Equality is based on val and unit. NaN is equal to itself (like Float.compare, but unlike Float.equals)

fromStr

static new fromStr(Str s, Bool checked := true)

Parse from a string according to zinc syntax

hash

virtual override Int hash()

Hash is based on val

increment

@Operator
Number increment()

Increment this number. Shortcut is ++a.

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

new make(Float val, Unit? unit := null)

Construct from scalar value and optional unit.

makeDuration

new makeDuration(Duration dur, Unit? unit := @hr)

Construct from a duration, standardize unit is hours If unit is null, then a best attempt is made based on magnitude.

makeInt

static new makeInt(Int val, Unit? unit := null)

Construct from scalar integer and optional unit.

makeNum

static Number makeNum(Num val, Unit? unit := null)

Construct from scalar Int, Float, or Decimal and optional unit.

max

Number max(Number that)

Return max value. Units are not checked for this comparison.

min

Number min(Number that)

Return min value. Units are not checked for this comparison.

minus

@Operator
Number minus(Number b)

Subtract b from this. Shortcut is a-b. The b.unit must match this.unit.

mod

@Operator
Number mod(Number b)

Return remainder of this divided by b. Shortcut is a%b. The unit of b must be null.

mult

@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.

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

@Operator
Number negate()

Negate this number. Shortcut is -a.

one

const static Number one := Number.<ctor>(1.0)

Constant for 1 with no unit

plus

@Operator
Number plus(Number b)

Add this with b. Shortcut is a+b. Throw UnitErr is this and b have incompatible units.

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

Str toCode()

Trio/zinc code representation, same as toStr

toDuration

Duration? toDuration(Bool checked := true)

Get this number as a Fantom Duration instance

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:

  1. If isDuration true, then return best fit unit is selected
  2. If unit is non-null attempt to lookup a unit specific default pattern with the locale key "haystack::number.{unit.name}".
  3. If isInt true, then return Int.toLocale using sys locale default
  4. 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