Ada fixed-point and decimal types
last modified November 23, 2026
Floating-point values are fast and wide-ranging, but most of them are not
exact: 0.1 has no finite binary representation. Ada offers a
second family of real types for the cases where exactness matters. A
fixed-point type stores a value as an integer multiple of a small unit, and a
decimal fixed-point type uses a power of ten as that unit, which makes it the
natural choice for money. This article declares both kinds, looks at their
precision, and prints amounts with currency pictures.
A fixed-point type
A fixed-point declaration names a delta, the precision the
program wants, and usually a range. The compiler picks a binary
Small that is at least as fine as delta, and every
value of the type is an integer multiple of that small. The
'Delta and 'Small attributes report the two values.
The example uses only quantities that are exact multiples of the small, so no
rounding warning is produced.
with Ada.Text_IO; use Ada.Text_IO;
procedure Fixed_Type is
type Voltage is delta 0.001 range -10.0 .. 10.0;
V : Voltage := 3.25;
begin
Put_Line ("delta =" & Float'Image (Float (Voltage'Delta)));
Put_Line ("small =" & Float'Image (Float (Voltage'Small)));
Put_Line ("value =" & Voltage'Image (V));
V := V + 0.25;
Put_Line ("value =" & Voltage'Image (V));
end Fixed_Type;
The requested delta is one thousandth, but the actual small is
0.0009765625, or 2-10, because binary fixed-point
types use a power of two. The two values in the program, 3.25 and 3.50, are
exact multiples of that small, so they are stored without loss and printed
with three decimal places. The Float conversions around the
attributes are needed because 'Delta and 'Small are
not of type Float.
$ gnatmake -gnat2022 -gnatwa fixed_type.adb $ ./fixed_type delta = 1.00000E-03 small = 9.76563E-04 value = 3.250 value = 3.500
Rounding to the small
A value that falls between two multiples of the small has to be moved to one
of them. On this GNAT build the conversion from Float drops the
fraction below one small, so 3.14159 becomes
3.1406250, which is 3216 times the small. Printing that stored
value with the declared precision rounds it to three decimal places and shows
3.141. The next program prints both the stored value and its
image.
with Ada.Text_IO; use Ada.Text_IO;
procedure Rounding is
package Float_IO is new Ada.Text_IO.Float_IO (Float);
type Voltage is delta 0.001 range 0.0 .. 10.0;
function To_Voltage (X : Float) return Voltage is
begin
return Voltage (X);
end To_Voltage;
Reading : constant Float := 3.14159;
V : constant Voltage := To_Voltage (Reading);
begin
Put_Line ("reading =" & Float'Image (Reading));
Put ("stored = ");
Float_IO.Put (Float (V), Fore => 1, Aft => 7, Exp => 0);
New_Line;
Put_Line ("image =" & Voltage'Image (V));
end Rounding;
The function To_Voltage is not decoration: it keeps the
conversion out of the constant declarations, so the compiler treats it as a
run-time operation and does not warn about the literal not being a multiple of
the small. The loss happens once, at the conversion, and every later operation
on a Voltage stays on the grid of multiples. For a sensor reading
this is acceptable; for a bank balance the lost fraction is not.
$ gnatmake -gnat2022 -gnatwa rounding.adb $ ./rounding reading = 3.14159E+00 stored = 3.1406250 image = 3.141
Decimal fixed point for money
A decimal fixed-point type adds digits, which fixes the small to
a power of ten and guarantees a given number of decimal digits of precision.
delta 0.01 digits 12 has a small of exactly one cent and
represents amounts with two decimal places without binary error. The program
computes a price with twenty percent tax and also compares the same addition
in Float and in the decimal type.
with Ada.Text_IO; use Ada.Text_IO;
procedure Money is
package Float_IO is new Ada.Text_IO.Float_IO (Float);
type Money is delta 0.01 digits 12;
Price : constant Money := 19.95;
Tax : constant Money := 0.20;
Total : constant Money := Price + Price * Tax;
Tenth : constant Money := 0.10;
Fifth : constant Money := 0.20;
begin
Put_Line ("price =" & Money'Image (Price));
Put_Line ("tax =" & Money'Image (Price * Tax));
Put_Line ("total =" & Money'Image (Total));
Put ("float 0.1 + 0.2 = ");
Float_IO.Put (0.1 + 0.2, Fore => 1, Aft => 10, Exp => 0);
New_Line;
Put_Line ("money 0.1 + 0.2 =" & Money'Image (Tenth + Fifth));
end Money;
Tax on 19.95 is exactly 3.99, and the total is exactly 23.94; no cent is lost
or invented. The float line shows why the decimal type exists: the sum that
should be 0.3 is stored as 0.3000000119, because the two binary fractions are
each slightly off. The constants Tenth and Fifth have the decimal
type, so the addition is performed in cents and the result is the exact 0.30, not a
universal real converted afterwards.
$ gnatmake -gnat2022 -gnatwa money.adb $ ./money price = 19.95 tax = 3.99 total = 23.94 float 0.1 + 0.2 = 0.3000000119 money 0.1 + 0.2 = 0.30
Cents as integers
Many interfaces, from payment APIs to database columns, exchange money as an
integer number of cents. Converting a decimal fixed-point value to
Integer multiplies by the small, which is exactly 0.01, so the
result is the number of cents. Dividing an integer number of cents by 100
reconstructs the amount. Both directions are exact as long as the value fits
in the integer type.
with Ada.Text_IO; use Ada.Text_IO;
procedure Cents is
type Money is delta 0.01 digits 12;
Total : constant Money := 23.94;
Cents : constant Integer := Integer (Total * 100);
Back : constant Money := Money (Cents) / 100;
begin
Put_Line ("total =" & Money'Image (Total));
Put_Line ("cents =" & Integer'Image (Cents));
Put_Line ("back =" & Money'Image (Back));
end Cents;
Total * 100 stays in the Money type, so it is still
an exact multiple of a cent, and the conversion to Integer yields
2394. Money (Cents) turns the integer back into an amount, and
the division by 100 returns 23.94. A program that adds many amounts should
prefer the integer representation for storage and convert to
Money only for display, which keeps every amount in one
well-defined unit.
$ gnatmake -gnat2022 -gnatwa cents.adb $ ./cents total = 23.94 cents = 2394 back = 23.94
Currency pictures
Ada.Text_IO.Editing formats decimal fixed-point values with a
picture string. The package is generic over a decimal type, so the program
instantiates Decimal_Output for Money. In the
picture, $ is the currency symbol, each Z is a digit
position that becomes a space when it is a leading zero, each 9
is a digit position that is always printed, and . is the decimal
point. The option Blank_When_Zero turns a zero amount into
spaces, which is useful in a report.
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Text_IO.Editing; use Ada.Text_IO.Editing;
procedure Editing is
type Money is delta 0.01 digits 12;
package Money_Output is new Decimal_Output (Money);
Pic : constant Picture := To_Picture ("$ZZZ9.99");
Blank : constant Picture :=
To_Picture ("$ZZZ9.99", Blank_When_Zero => True);
begin
Put_Line (Money_Output.Image (1234.56, Pic));
Put_Line (Money_Output.Image (9.5, Pic));
Put_Line (Money_Output.Image (0.05, Pic));
Put_Line ("[" & Money_Output.Image (0.00, Blank) & "]");
end Editing;
The first amount fills all the integer positions. The second leaves three
spaces where the leading zeros would be, and the third leaves three spaces
before the zero that must be printed. The last line uses the blank picture,
so the brackets enclose eight spaces and no currency symbol. The picture is
checked when To_Picture is called; a malformed string raises
Picture_Error instead of producing a wrong amount.
$ gnatmake -gnat2022 -gnatwa editing.adb $ ./editing $1234.56 $ 9.50 $ 0.05 [ ]
Next steps
The next article leaves the range of the built-in numeric types behind and
computes with integers of unlimited size: it raises two to the thousandth
power, calculates 100 factorial, sums the digits of a huge number, and
represents one third as an exact rational value. The
types article introduces the scalar types, and
text output explains the
Float_IO formatting used for the comparisons above.
Source
Ada Reference Manual: Fixed Point Types
Ada Reference Manual: The Package Text_IO.Editing
In this article we have declared binary and decimal fixed-point types, inspected their delta and small, seen a float reading truncated to a multiple of the small, kept a price and its tax in exact cents, converted money to and from integer cents, and printed currency with editing pictures. The operators article explains the arithmetic that these types share with the other numeric types.
Author
List all Ada tutorials.