Number type
  • 22 Feb 2024
  • 3 Minutes to read
  • Contributors
  • Dark
    Light

Number type

  • Dark
    Light

Article summary

Numbers are used for all kinds of calculations. There are literal numbers, like 7 or 1.5, but a component or variable can also be of type number. Both kinds can be used interchangeably and everything described on this page applies to either kind.
For example, you can call a function like round() on a component: price.round() or on a literal number: 13.5.round().

Operations

The following arithmetic operations are available in Hive (the examples assume that there is a component price with a value of 5.

name

example

result

description

negate

-price

-5

gets the inverse of a number

add

price + 2

7

adds two numbers

substract

2 - price

-3

subtracts one number from another

multiply

2 * price

10

multiplies two numbers

divide

2 / price

2.5

divides one number by another

exponentiation

price ^ 2

25

raises a number to the power of another number

modulo

price mod 2

1

divides one number by another and returns the remainder

Comparing numbers

In addition to the above, there are also six operators for comparing numbers.

name

example

result

description

greater than

price > 5

false

returns true if the first number is greater than the second

greater than or equal

price >=

true

returns true if the first number is greater than or equal to the second

less than

4 < price 

true

returns true if the first number is less than the second

less than or equal

4 <= price

false

returns true if the first number is less than or equal to the second

equal to

price = 5

true

returns true if one number is equal to another

not equal to 

price <> 19

true

returns true if one number is not equal to another

Functions

name

description

round(digits)


rounds a number to the nearest integer or to the specified number of fractional digits

floor

gets the largest integer less than or equal to the specified number

ceiling

gets the smallest integer greater than or equal to the specified number

truncate

calculates the integral part of a number

atLeast(min)


gets a number that is greater than or equal to the specified threshold

atMost(max)


gets a number that is less than or equal to the specified threshold

toText(format,locale)


converts a number to text

For additional mathematical functions check out the article about the math type.

round

Rounds a number to the specified number of fractional digits, away from zero.
If no parameter is used, or the number of digits is zero, rounds to the nearest integer.

example

result

price.round()

4

price.round(1)

4.4

price.round(2)

4.45

If you specify a negative number of digits, its positive inverse will be used, i.e. round(-2) is the same as round(2)


floor

Returns the largest integer that is less than or equal to the specified number.

example

result

1.1.floor()

1

1.9.floor()

1

(-1.1).floor()

-2


ceiling

Returns the smallest integer that is greater than or equal to the specified number.

example

result

1.1.ceiling()

1

1.3.ceiling()

2

(-1.1).ceiling()

-1


truncate

Calculates the integral part of a number by rounding it to the nearest integer towards zero.

example

result

1.1.truncate()

1

1.9.truncate()

1

(-1.9).truncate()

-1


atLeast(min)

Compares a number to a specified threshold and returns it if it is greater than or equal to the threshold, otherwise returns the threshold.

example

result

5.atLeast(10)

10

5.atLeast(3)

5


atMost(max)

Compares a number to a specified threshold and returns it if it is less than or equal to the threshold, otherwise returns the threshold.

example

result

5.atMost(2)

2

5.atMost(10)

5


toText(format,locale)

Converts a number to text. You can optionally customize the conversion with the format parameter and specify the desired locale.

example

result

price.toText()

"4.445"

price.toText("€ 0.##")

"€ 4.45"

1337.toText("$ 0,0.00")

"$ 1,337.00"

1337.toText("€ 0,0.00", Locale.de)


"€ 1.337,00"

You may use a combination of the following characters to customize the conversion of numbers to text.

zero placeholder (0)

Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result text.
123.456.toText("0000") becomes 0123
123.toText("000.00") becomes 123.00

digit placeholder (#)

Replaces the hash sign with the corresponding digit if one is present; otherwise, no digit appears in the result text.
123.456.toText("####") becomes 123
123.toText("###.##") becomes 123

decimal placeholder (.)

Determines the location of the decimal separator in the result text.
0.1234.toText("0.00") becomes 0.12

group separator placeholder (,)

Inserts a localized group separator between each number group in the integral part of the output.
12345678.toText("##,#") becomes 12,345,678

percentage placeholder (%)

Multiplies a number by 100 and inserts a percentage symbol in the result text.
0.35.toText("0%") becomes 35%

section separator (;)

Defines sections with separate format strings for positive, negative and zero numbers.
(-13.37).toText("##.##;(##.##)") becomes (13.37)

Other characters

All characters that have not been mentioned above will simply be copied to the result text:
24.toText("0 degrees") becomes 24 degrees

In order to use placeholder characters literally, you can precede them with a backslash:
13.37.toText("\# 00.0") becomes # 13.4

You can also use single quotes ' or double quotes " to enclose literal text that should be copied to the result:
13.37.toText("00.0'% of total'") becomes 13.4% of total

Rounding

If the formatted value contains more significant digits than are present in the format text, the value will be rounded to the nearest digit and away from zero.
10.5.toText("#") becomes 11
0.25.toText("0.0") becomes 0.3


Was this article helpful?

What's Next