lang.decimal
ballerina/lang.decimal Ballerina library
Module overview
The lang.decimal
module corresponds to the decimal
basic type.
Functions
abs
Returns the IEEE absolute value of a decimal value.
decimal d = -3.21; d.abs() ⇒ 3.21
Parameters
- x decimal - decimal value to operate on
Return Type
- decimal - absolute value of parameter
x
avg
Returns the average of its arguments.
decimal:avg(10, 20, 30, 40) ⇒ 25.0
Return Type
- decimal - average of parameter
x
and all of parameterxs
ceiling
Rounds a decimal up to the closest integral value.
decimal d = 3.51; d.ceiling() ⇒ 4
Parameters
- x decimal - decimal value to operate on
Return Type
- decimal - smallest (closest to -∞) decimal value not less than parameter
x
that is a mathematical integer
floor
Rounds a decimal down to the closest integral value.
decimal d = 3.51; d.floor() ⇒ 3
Parameters
- x decimal - decimal value to operate on
Return Type
- decimal - largest (closest to +∞) decimal value not greater than parameter
x
that is a mathematical integer.
fromString
Returns the decimal value represented by a string.
s
must follow the syntax of DecimalFloatingPointNumber as defined by the Ballerina specification
with the following modifications
- the DecimalFloatingPointLiteral may have a leading
+
or-
sign - a FloatingPointTypeSuffix is not allowed
This is the inverse of function
value:toString
applied to andecimal
.
decimal:fromString("0.2453") ⇒ 0.2453 decimal:fromString("-10") ⇒ -10 decimal:fromString("123d") ⇒ error
Parameters
- s string - string representation of a decimal
max
Returns the maximum of one or more decimal values.
decimal:max(1.2, 12.3, 3.4) ⇒ 12.3 decimal[] marks = [70.3, 80.5, 98.1, 92.3]; decimal:max(30.5, ...marks) ⇒ 98.1 [decimal, decimal, decimal] scores = [7.21, 10.32, 9.2]; decimal:max(...scores) ⇒ 10.32 decimal d = 21.2; d.max(40.5, 21, 32.4) ⇒ 40.5
Return Type
- decimal - maximum value of parameter
x
and all the parameterxs
min
Returns the minimum of one or more decimal values.
decimal:min(5.2, 2.3, 3.4) ⇒ 2.3 decimal[] marks = [90.3, 80.5, 98, 92.3]; decimal:min(82.1, ...marks) ⇒ 80.5 [decimal, decimal, decimal] scores = [7.21, 10.32, 9.2]; decimal:min(...scores) ⇒ 7.21 decimal d = 1.2; d.min(10.5, 21, 32.4) ⇒ 1.2
Return Type
- decimal - minimum value of parameter
x
and all the parameterxs
.
quantize
Return a decimal with a specified value and exponent. Return a decimal value that has the same value (except for rounding) as the first argument, and the same exponent as the second argument. This is the IEEE quantize operation.
decimal d = 4.1626; d.quantize(3.512) ⇒ 4.163 decimal:quantize(4.1626, 3.512) ⇒ 4.163 decimal:quantize(4.1624, 3.51) ⇒ 4.16
Parameters
- x decimal - decimal value to operate on
- y decimal - decimal value from which to get the quantum
Return Type
- decimal -
x
with the quantum ofy
round
Round a decimal to a specified number of digits.
Returns the decimal value that has an exponent of -fractionDigits
and is closest to x
.
If there are two such values, returns the one whose final digit is even
(this is the round-to-nearest rounding mode, which is the default for IEEE
and for Ballerina).
A value of fractionDigits
greater than 0 thus corresponds to the number of digits after the decimal
point being fractionDigits
; a value of 0 for fractionDigits
rounds to an integer.
Note that IEEE 754 roundToIntegralTiesToEven operation differs from round
with fractionDigits
as 0, in that
roundToIntegralTiesToEven will return a value with a positive exponent when the operand has a positive exponent.
Note that <int>x
is the same as <int>x.round(0)
.
decimal d = 3.55; d.round() ⇒ 4 decimal e = 4.55555; e.round(3) ⇒ 4.556 decimal g = 2.5; g.round(0) ⇒ 2 decimal h = 3.5; h.round(0) ⇒ 4 decimal e = 4345.55; e.round(-3) ⇒ 4E+3
Parameters
- x decimal - decimal value to operate on
- fractionDigits int (default 0) - the number of digits after the decimal point
Return Type
- decimal - closest decimal value to
x
that is an integral multiple of 10 raised to the power of-fractionDigits
sum
Returns the sum of zero or more decimal values.
decimal:sum(1.2, 2.3, 3.4) ⇒ 6.9 decimal[] scores = [11.1, 22.2, 33.3]; decimal:sum(...scores) ⇒ 66.6 [decimal, decimal, decimal] marks = [7.21, 10.32, 9.2]; decimal:sum(...marks) ⇒ 26.73 decimal d = 21.2; d.sum(10.5, 21, 32.4) ⇒ 85.1
Parameters
- xs decimal... - decimal values to sum
Return Type
- decimal - sum of all the parameter
xs
; 0 if parameterxs
is empty