import ballerina/io;

// The `decimal` type represents the set of 128-bits IEEE 754R decimal floating point numbers.
decimal nanos = 1d/1000000000d;

function floatSurprise() {
    float f = 100.10 - 0.01;
    io:println(f);
}

public function main() {
    floatSurprise();
    io:println(nanos);
}

Decimal Type

It is the third numeric type. Works like int and float. No implicit conversion. Represents decimal fractions exactly. Avoids surprises that you get with float. Preserves precision. Separate basic type; counts as anydata. Literal uses the suffix d(f suffix is for float). Floating point, not infinite precision.

  • 34 decimal digits
  • 22 digits are enough for US national debt in ยข
  • 27 digits are enough for an age of universe in ns
  • No infinity, NaN or `negative zero

import ballerina/io;
decimal nanos = 1d/1000000000d;

The decimal type represents the set of 128-bits IEEE 754R decimal floating point numbers.

function floatSurprise() {
    float f = 100.10 - 0.01;
    io:println(f);
}
public function main() {
    floatSurprise();
    io:println(nanos);
}
bal run decimal_type.bal
100.08999999999999
1E-9