Float
Decimal
Literals
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.
|
|
bal run float_literal_float_type.bal
12.345
-5.678
0.5678
123.456
567.8901
505.0
505.0
bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
Floating point literal with the float type
|
|
bal run float_literal_decimal_type.bal
12.345
-5.678
0.5678
123.456
567.8901
505
505.0
bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
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.
bal run float_literal_hex.bal
5.5
-5.5
123.456
123.456
505.625
bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
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 =
+
|-
|
|
bal run float_literal_exponent.bal
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;
}
Floating point literal with the exponent