Skip to content

Number

Type

The Number type is used to represent arbitrary-precision signed decimal numbers (including integers), which are basically of the Java BigDecimal type. Literals of the Number type are exactly represented in memory with an unscaled arbitrary precision integer and a 32-bit integer scale (representation: unscaledValue * 10^(-scale)). We might not be able to represent the result of a division exactly in memory. Therefore we use a precision of 16 digits with HALF_UP rounding (round half away from zero).

A Number variable can be initialized or mutated by assigning it an integer or decimal literal or an expression of the Number type, e.g.

var e = 0.0000005;
var f = 3000;
var g = 10.0;
var d = 123456.789;
d = d + 44.1 * 3.14;

Methods

divideBy

<function> Divides this number with another number. Infix operator: /.

Receiver

Number

Arguments

other - Number - number to divide this with

Returns

Number - the result of dividing this with other, can have a fractional part

Usage

20 / 5 == 4
2 / 4 == 0.5

greaterThan

<function> Compare two numbers. Infix operator: >.

Receiver

Number

Arguments

other - Number

Returns

Boolean - true if this is greater than other, otherwise false

greaterThanOrEqual

<function> Compare two numbers. Infix operator: >=.

Receiver

Number

Arguments

other - Number

Returns

Boolean - true if this is greater than or equal to other, otherwise false

isInteger

<function> Returns a Boolean value that indicates whether the Number is equivalent to an integer (has no fractional part different from 0).

Receiver

Number

Returns

Boolean

Usage

51.4.isInteger() == false
51.000.isInteger() == true

lessThan

<function> Compare two numbers. Infix operator: <.

Receiver

Number

Arguments

other - Number

Returns

Boolean - true if this is less than other, otherwise false

lessThanOrEqual

<function> Compare two numbers. Infix operator: <=.

Receiver

Number

Arguments

other - Number

Returns

Boolean - true if this is less than or equal to other, otherwise false

negative

<function> Negates this number.

Receiver

Number

Returns

Number

Usage

(20).negative() == (-20)
(-5).negative() == 5

minus

<function> Subtracts another number from this one. Infix operator: -.

Receiver

Number

Arguments

other - Number - number to subtract from this

Returns

Number - the difference between this and other

multiplyBy

<function> Multiplies this number with another number. Infix operator: *.

Receiver

Number

Arguments

other - Number - number to multiply this with

Returns

Number - the product of this and other

plus

<function> Adds another number to this one. Infix operator: +.

Receiver

Number

Arguments

other - Number - number to add to this

Returns

Number - the sum of this and other

remainder

<function> Returns the remainder of dividing this number with another number. Infix operator: %.

Receiver

Number

Arguments

other - Number - number to divide this with

Returns

Number - the remainder when dividing this with other

roundTo

<function> Rounds this number to a Number with (non-negative) roundTo digits behind the decimal point. Rounding method used is HALF_UP. roundTo(0) rounds to an integer value.

Receiver

Number

Arguments

roundTo - Number

Returns

Number

Usage

(51.7654).roundTo(1) == 51.8
(51.7654).roundTo(3) == 51.765
(51.7654).roundTo(0) == 52
(-51.7654).roundTo(2) == -51.77
(0.0045678).roundTo(4) == 0.0046

Inherited methods

toText

<function> Obtain the Text representation of this.

Receiver

T

Type Arguments

T

Returns

Text - the Text representation of this

Usage

Converts a Number to Text.

(51.4).toText() == "51.4"
(0.345).toText() == "0.345"
3000.toText() == "3000"