Swan Lake Beta2 is here.
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
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

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand