class

Number

@Js
@Serializable { simple=true }
const class Number : Obj

Number represents a numeric value and an optional Unit.

constructors makeInt

Construct from scalar integer and optional unit

fromStr

Parse from a string according to zinc syntax

makeDuration

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

make

Construct from scalar value and optional unit

fields zero

Constant for 0 with no unit

negOne

Constant for -1 with no unit

negInf

Constant for negative infinity

one

Constant for 1 with no unit

nan

Constant for not-a-number

ten

Constant for 10 with no unit

posInf

Constant for positive infinity

defVal

Default value is zero with no unit

methods minus

Subtract b from this

compare

Compare is based on val

mod

Return remainder of this divided by b

mult

Multiple this and b

upper

Get the ASCII upper case version of this number as a Unicode point

increment

Increment this number

isNaN

Is the floating value NaN

div

Divide this by b

min

Return min value

isSpecial

Return if this number if pos/neg infinity or NaN

isInt

Is this number a whole integer without a fractional part

makeNum

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

clamp

Clamp this number between the min and max

toStr

String representation

isNegative

Is this a negative number

toInt

Get the scalar value as an Int

toLocale

Format the number using given pattern which is an superset of Float.toLocale

isDuration

Does this number have a time unit which can be converted to a Fantom Duration instance

max

Return max value

toCode

Trio/zinc code representation, same as toStr

lower

Get the ASCII lower case version of this number as a Unicode point

toFloat

Get the scalar value as an Float

plus

Add this with b

unit

Get unit associated with this number or null

abs

Return absolute value of this number

decrement

Decrement this number

negate

Negate this number

equals

Equality is based on val and unit

toDuration

Get this number as a Fantom Duration instance

hash

Hash is based on val

approx

Return if this number is approximately equal to that - see Float.approx

abs Number abs()

Return absolute value of this number.

approx Bool approx(Number that, Float? tolerance)

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

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 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)

Parse from a string according to zinc syntax

hash virtual 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)

Construct from scalar value and optional unit.

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

makeInt static new makeInt(Int val, Unit? unit)

Construct from scalar integer and optional unit.

makeNum static Number makeNum(Num val, Unit? unit)

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

Constant for not-a-number

negInf const static Number : negInf

Constant for negative infinity

negOne const static Number : negOne

Constant for -1 with no unit

negate @Operator
Number negate()

Negate this number. Shortcut is -a.

one const static Number : one

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

Constant for positive infinity

ten const static Number : ten

Constant for 10 with no unit

toCode Str toCode()

Trio/zinc code representation, same as toStr

toDuration Duration? toDuration(Bool checked)

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)

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

Constant for 0 with no unit

Haxall 4.0.5 ∙ 24-Feb-2026 14:33 EST