lang.decimal
ballerina/lang.decimal
Module overview
The lang.decimal
module corresponds to the decimal
basic type.
Functions
abs
Returns the IEEE absolute value of a decimal value.
Parameters
- x decimal - decimal value to operate on
Return Type
- decimal - absolute value of parameter
x
avg
Returns the average of its arguments.
Return Type
- decimal - average of parameter
x
and all of parameterxs
ceiling
Rounds a decimal up to the closest integral value.
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.
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
.
Parameters
- s string - string representation of a decimal
max
Returns the maximum of one or more decimal values.
Return Type
- decimal - maximum value of parameter
x
and all the parameterxs
min
Returns the minimum of one or more decimal values.
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.
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)
.
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.
Parameters
- xs decimal... - decimal values to sum
Return Type
- decimal - sum of all the parameter
xs
; 0 if parameterxs
is empty