Decimal
Types
ballerina/lang.decimal
Post
Decimal Data Type
The Decimal basic data type represents 128-bit IEEE 754-2008 decimal floating point values. Decimals in Ballerina provide higher precision and a larger range of values than floating-point numbers. Which means, they are suitable for financial and monetary calculations.
✍ Syntax
decimal
✍ Syntax
The decimal type is written as decimal
. You can use Floating Point Literal to represent a decimal value. Please note that you cannot use hexadecimal floating point literals with decimal
type.
|
|
bal run decimal_intro.bal
Positive decimal:10.5
Negative decimal:-5.25
Sum of decimals:5.25
Difference of decimals:15.75
Product of decimals:-55.125
Quotient of decimals:-2
bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
// Partial Code. Change View to see full code.
public function main() {
decimal positiveDecimal = 10.5;
decimal negativeDecimal = -5.25;
decimal sum = positiveDecimal + negativeDecimal;
decimal difference = positiveDecimal - negativeDecimal;
decimal product = positiveDecimal * negativeDecimal;
decimal quotient = positiveDecimal / negativeDecimal;
}
Decimal Literal
Properties and Operations
The decimal
type supports the following operations and many more language features. I will discuss these operations in detail in later posts.
- Unary expressions -
+ x
,- x
- Multiplicative expressions -
x * y
,x / y
,x % y
- Additive expressions -
x + y
,x - y
- Relational expressions -
x < y
,x > y
,x <= y
,x >= y
- Equality expressions -
x == y
,x != y
,x === y
,x !=== y
- Type Cast expression -
<int> x
,<float> x
Unlike some programming languages, there is no implicit conversion between Ballerina numerical types. Learn more.
|
|
bal run decimal_operation.bal
Unary, Arithmetic, and Relational Operations:
+x: 5.5
-x: -5.5
x + y: 15.5
x - y: -4.5
x * y: 55.00
x / y: 0.55
x % y: 5.5
x < y: true
x <= y: true
x > y: false
x >= y: false
x == y: false
x != y: true
Type Cast Expressions:
int(a): 6
float(a): 5.5
bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
// Partial Code. Change View to see full code.
public function main() {
decimal x = 5.5;
decimal y = 10.0;
string output = string `Unary, Arithmetic, and Relational Operations:
+x: ${+x}
-x: ${-x}
x + y: ${x + y}
x - y: ${x - y}
x * y: ${x * y}
x / y: ${x / y}
x % y: ${x % y}
x < y: ${x < y}
x <= y: ${x <= y}
x > y: ${x > y}
x >= y: ${x >= y}
x == y: ${x == y}
x != y: ${x != y}
`;
output = string `Type Cast Expressions:
int(a): ${<int>x}
float(a): ${<float>x}
`;
}
// Highlights Only. Change View to see full code.
decimal x = 5.5;
decimal y = 10.0;
string output = string `Unary, Arithmetic, and Relational Operations:
+x: ${+x}
-x: ${-x}
x + y: ${x + y}
x - y: ${x - y}
x * y: ${x * y}
x / y: ${x / y}
x % y: ${x % y}
x < y: ${x < y}
x <= y: ${x <= y}
x > y: ${x > y}
x >= y: ${x >= y}
x == y: ${x == y}
x != y: ${x != y}
`;
output = string `Type Cast Expressions:
int(a): ${<int>x}
float(a): ${<float>x}
`;
Decimal Operations
decimal
values do not have a storage identity, so they cannot be modified once created. As a result, they can be assigned to a readonly
variable like other numeric types, such as int
and float
.
The decimal
type is part of the anydata
and json
. Also, it is an ordered type.
Language Library - ballerina/lang.decimal
ballerina\lang.decimal
Lang Library provides utility functions for decimal
type. See available operations with examples in Decimal Lang Library.
Additionally, ballerina/lang.value
Lang Library provides toString
and another set of functions that can operate on a decimal
value.
|
|
bal run decimal_lib.bal
ballerina/lang.decimal Functions:
x: 5.5
y: -10.0
max(x, y): 5.5
min(x, y): -10.0
abs(y): 10.0
round(x): 6
bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
Decimal Lang Library
Conclusion
In this post, I have discussed the decimal
data type in Ballerina. Here are the key points:
- Floating Point Literal, e.g.
12.34
- No implicit conversion between numbers.
- Lang Library : ballerina/lang.decimal.