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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import ballerina/io;

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;

    io:println("Positive decimal:", positiveDecimal);
    io:println("Negative decimal:", negativeDecimal);
    io:println("Sum of decimals:", sum);
    io:println("Difference of decimals:", difference);
    io:println("Product of decimals:", product);
    io:println("Quotient of decimals:", quotient);
}
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;
}
// Highlights Only. Change View  to see full code.
decimal positiveDecimal = 10.5;
decimal negativeDecimal = -5.25;

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import ballerina/io;

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}
    `;
    io:println(output);

    output = string `Type Cast Expressions:
    int(a): ${<int>x}
    float(a): ${<float>x}
    `;
    io:println(output);
}
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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import ballerina/io;

public function main() {

    decimal x = 5.5;
    decimal y = -10.0;

    decimal maxVal = decimal:max(x, y);
    decimal minVal = decimal:min(x, y);
    decimal absY = y.abs();
    decimal roundVal = x.round();

    string output = string `ballerina/lang.decimal Functions:
    x: ${x}
    y: ${y}
    max(x, y): ${maxVal}
    min(x, y): ${minVal}
    abs(y): ${absY}
    round(x): ${roundVal}
    `;
    io:println(output);
}
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)
// Partial Code. Change View  to see full code.
public function main() {
    decimal x = 5.5;
    decimal y = -10.0;

    decimal maxVal = decimal:max(x, y);
    decimal minVal = decimal:min(x, y);
    decimal absY = y.abs();
    decimal roundVal = x.round();
}
// Highlights Only. Change View  to see full code.
decimal x = 5.5;
decimal y = -10.0;

decimal maxVal = decimal:max(x, y);
decimal minVal = decimal:min(x, y);
decimal absY = y.abs();
decimal roundVal = x.round();

Decimal Lang Library

Conclusion

In this post, I have discussed the decimal data type in Ballerina. Here are the key points:


Decimal Data Type

Navigation

Site Settings

Site Theme

Source Code Density