Post

Floating Point Literal

Floating point values can be expressed in either Decimal (base 10) or Hexadecimal (base 16) systems and come in two types, float and decimal, with slight differences in their representation using floating point literals.

✍ Syntax

Decimal Literal

[DecimalNumber] . Digit+ [f|F|d|D]

Here

DecimalNumber = 0 | NonZeroDigit Digit*


✍ Syntax

Hexadecimal Literal

HexIndicator [HexDigit+] . HexDigit+

Here

HexIndicator = 0x|0X


✍ Syntax

Floating Point Literal With Exponent
  • DecimalNumber Exponent [f|F|d|D]
  • [DecimalNumber] . Digit+ Exponent [f|F|d|D]
  • HexIndicator HexDigit+ HexExponent
  • HexIndicator [HexDigit+] . HexDigit+ HexExponent

Here

  • DecimalNumber = 0 | NonZeroDigit Digit*
  • Exponent = ExponentIndicator [Sign] Digit+
  • HexExponent = HexExponentIndicator [Sign] Digit+
  • ExponentIndicator = e|E
  • HexIndicator = 0x|0X
  • HexExponentIndicator = p|P
  • Sign = +|-

✍ Syntax

Decimal Floating Point Literal

[DecimalNumber] . Digit+ [f|F|d|D]

Here

DecimalNumber = 0 | NonZeroDigit Digit*

You can write floating point values using a set of digits of the decimal system (Base 10 number) numbers, by using a dot . as the radix character to separate integer-part and fractional-part.

Optionally you can write float-suffix character f or F precisely to say the numerical literal is a float value, Or decimal-suffix character d or D to say the numerical literal is a decimal value.

Digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. You can make a floating point number negative by writing a minus (-) sign in front of the literal. See Unary Expressions for more details.

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

public function main() {

    float f1 = 12.345;
    float f2 = -5.678;
    float f3 = .5678;
    float f4 = 123.456f;
    float f5 = 567.8901F;
    float f6 = 505f;
    float f7 = 505.0f;

    io:println(f1);
    io:println(f2);
    io:println(f3);
    io:println(f4);
    io:println(f5);
    io:println(f6);
    io:println(f7);
}
12.345
-5.678
0.5678
123.456
567.8901
505.0
505.0
 bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
// Partial Code. Change View  to see full code.
public function main() {
    float f1 = 12.345;
    float f2 = -5.678;
    float f3 = .5678;
    float f4 = 123.456f;
    float f5 = 567.8901F;
    float f6 = 505f;
    float f7 = 505.0f;
}
// Highlights Only. Change View  to see full code.
float f1 = 12.345;
float f2 = -5.678;
float f3 = .5678;
float f4 = 123.456f;
float f5 = 567.8901F;
float f6 = 505f;
float f7 = 505.0f;

Floating point literal with the float type

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

public function main() {

    decimal f1 = 12.345;
    decimal f2 = -5.678;
    decimal f3 = .5678;
    decimal f4 = 123.456d;
    decimal f5 = 567.8901D;
    decimal f6 = 505d;
    decimal f7 = 505.0d;

    io:println(f1);
    io:println(f2);
    io:println(f3);
    io:println(f4);
    io:println(f5);
    io:println(f6);
    io:println(f7);
}
12.345
-5.678
0.5678
123.456
567.8901
505
505.0
 bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
// Partial Code. Change View  to see full code.
public function main() {
    decimal f1 = 12.345;
    decimal f2 = -5.678;
    decimal f3 = .5678;
    decimal f4 = 123.456d;
    decimal f5 = 567.8901D;
    decimal f6 = 505d;
    decimal f7 = 505.0d;
}
// Highlights Only. Change View  to see full code.
decimal f1 = 12.345;
decimal f2 = -5.678;
decimal f3 = .5678;
decimal f4 = 123.456d;
decimal f5 = 567.8901D;
decimal f6 = 505d;
decimal f7 = 505.0d;

Floating point literal with the decimal type

Hexadecimal Floating Point Literal

Hexadecimal numbers start with a Hex Indicator 0x (Number zero and lowercase letter x) or 0X (Number zero and Uppercase letter X) Hex digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a or A, b or B, c or C, d or D, e or E, f or F

HexIndicator [HexDigit+] . HexDigit+

Here

HexIndicator = 0x|0X

💡 Hexadecimal floating point literal only supports the float type.

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

public function main() {

    float f1 = 0x5.8;
    float f2 = -0x5.8;
    float f3 = 0x7b.74bc6a7ef9db22d;
    float f4 = 0X7B.74BC6A7EF9DB22D;
    float f5 = 0x1F9.A;

    io:println(f1);
    io:println(f2);
    io:println(f3);
    io:println(f4);
    io:println(f5);
}
5.5
-5.5
123.456
123.456
505.625
 bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
// Partial Code. Change View  to see full code.
public function main() {
    float f1 = 0x5.8;
    float f2 = -0x5.8;
    float f3 = 0x7b.74bc6a7ef9db22d;
    float f4 = 0X7B.74BC6A7EF9DB22D;
    float f5 = 0x1F9.A;
}
// Highlights Only. Change View  to see full code.
float f1 = 0x5.8;
float f2 = -0x5.8;
float f3 = 0x7b.74bc6a7ef9db22d;
float f4 = 0X7B.74BC6A7EF9DB22D;
float f5 = 0x1F9.A;

Hexadecimal floating point literal

Floating Point Literal With Exponent

You can write floating numbers using scientific notation. The Letter e or E is used as the exponent indicator in decimal format, while p or P is used as the exponent indicator in hexadecimal format.

  • DecimalNumber Exponent [f|F|d|D]
  • [DecimalNumber] . Digit+ Exponent [f|F|d|D]
  • HexIndicator HexDigit+ HexExponent
  • HexIndicator [HexDigit+] . HexDigit+ HexExponent

Here

  • DecimalNumber = 0 | NonZeroDigit Digit*
  • Exponent = ExponentIndicator [Sign] Digit+
  • HexExponent = HexExponentIndicator [Sign] Digit+
  • ExponentIndicator = e|E
  • HexIndicator = 0x|0X
  • HexExponentIndicator = p|P
  • Sign = +|-
 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
import ballerina/io;

public function main() {

    float f1 = 12e4;
    float f2 = 543e-5;
    float f3 = 1.23e3;
    float f4 = 1.23e3f;
    decimal d1 = 12e4;
    decimal d2 = 543e-5;
    decimal d3 = 1.23e3;
    decimal d4 = 1.23e3d;
    float fh1 = 0x1.a2bp3;
    float fh2 = 0X1F3Dp3;
    float fh3 = 0xAB12CDP-3;

    io:println(f1);
    io:println(f2);
    io:println(f3);
    io:println(f4);
    io:println(d1);
    io:println(d2);
    io:println(d3);
    io:println(d4);
    io:println(fh1);
    io:println(fh2);
    io:println(fh3);
}
120000.0
0.00543
1230.0
1230.0
1.2E+5
0.00543
1.23E+3
1.23E+3
13.083984375
63976.0
1401433.625
 bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
// Partial Code. Change View  to see full code.
public function main() {
    float f1 = 12e4;
    float f2 = 543e-5;
    float f3 = 1.23e3;
    float f4 = 1.23e3f;
    decimal d1 = 12e4;
    decimal d2 = 543e-5;
    decimal d3 = 1.23e3;
    decimal d4 = 1.23e3d;
    float fh1 = 0x1.a2bp3;
    float fh2 = 0X1F3Dp3;
    float fh3 = 0xAB12CDP-3;
}
// Highlights Only. Change View  to see full code.
float f1 = 12e4;
float f2 = 543e-5;
float f3 = 1.23e3;
float f4 = 1.23e3f;
decimal d1 = 12e4;
decimal d2 = 543e-5;
decimal d3 = 1.23e3;
decimal d4 = 1.23e3d;
float fh1 = 0x1.a2bp3;
float fh2 = 0X1F3Dp3;
float fh3 = 0xAB12CDP-3;

Floating point literal with the exponent

Floating Point Literal

Navigation

Site Settings

Site Theme

Source Code Density