ZetCode

Ada number formatting

last modified November 23, 2026

A number printed by itself is rarely enough. Reports need columns that line up, values in hexadecimal, prices with two digits after the point, and sometimes a completely different notation such as Roman numerals. Ada keeps these concerns in separate places: the 'Image attribute gives a quick textual form, the generic Integer_IO and Float_IO packages control width, base, and decimal places, and a small function can produce any other representation.

The leading space in 'Image

'Image reserves room for a minus sign even when the value is positive, so Integer'Image (42) is " 42", not "42". A negative value uses the sign position itself: Integer'Image (-7) is "-7". The brackets in the next program show the difference, and Trim with Ada.Strings.Left removes the padding when it is not wanted.

images.adb
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;

procedure Images is
begin
   Put_Line ("[" & Integer'Image (42) & "]");
   Put_Line ("[" & Integer'Image (-7) & "]");
   Put_Line ("[" & Trim (Integer'Image (42), Left) & "]");
   Put_Line ("[" & Trim (Integer'Image (-7), Left) & "]");
end Images;

The first line keeps the leading space, and the third line removes it. The negative value already starts with its sign, so trimming changes nothing. The same rule applies to the other scalar attributes and to 'Image of an enumeration, which is why printed enumeration values often line up neatly until the text is concatenated with something else.

$ gnatmake -gnat2022 -gnatwa images.adb
$ ./images
[ 42]
[-7]
[42]
[-7]

Bases and field widths

Ada.Text_IO.Integer_IO is a generic package that must be instantiated for the integer type. Its Put procedure accepts a Width and a Base. A base other than ten is written as a based literal, with the base first, a #, the digits, and a closing #. Width right-justifies the whole result, which is what makes a column of numbers line up.

bases.adb
with Ada.Text_IO; use Ada.Text_IO;

procedure Bases is
   package Int_IO is new Ada.Text_IO.Integer_IO (Integer);
begin
   Int_IO.Put (255, Width => 12, Base => 2);
   New_Line;
   Int_IO.Put (255, Width => 12, Base => 8);
   New_Line;
   Int_IO.Put (255, Width => 12, Base => 16);
   New_Line;
   Int_IO.Put (42, Width => 5);
   Int_IO.Put (7, Width => 5);
   New_Line;
end Bases;

255 is eight ones in binary, 377 in octal, and FF in hexadecimal. The last line shows the ordinary decimal output padded to five characters, so 42 and 7 end in the same column. Base sixteen uses uppercase letters by default; the Base parameter accepts values from 2 to 16.

$ gnatmake -gnat2022 -gnatwa bases.adb
$ ./bases
 2#11111111#
      8#377#
      16#FF#
   42    7

Floating-point fields

Float_IO.Put has three field parameters. Fore is the minimum number of digits before the decimal point, Aft is the exact number of digits after it, and Exp is the number of digits in the exponent; Exp => 0 asks for ordinary notation. When the integer part needs more room than Fore, the field simply grows, so a value is never truncated.

floats.adb
with Ada.Text_IO; use Ada.Text_IO;

procedure Floats is
   package Float_IO is new Ada.Text_IO.Float_IO (Float);
begin
   Float_IO.Put (3.14159, Fore => 1, Aft => 2, Exp => 0);
   New_Line;
   Float_IO.Put (3.14159, Fore => 1, Aft => 5, Exp => 0);
   New_Line;
   Float_IO.Put (12345.678, Fore => 1, Aft => 2, Exp => 0);
   New_Line;
   Float_IO.Put (0.000123, Fore => 1, Aft => 2, Exp => 3);
   New_Line;
end Floats;

The first two lines show the effect of Aft: two digits round 3.14159 to 3.14, while five digits keep 3.14159. The third value needs five digits before the point even though Fore is one, and it is still printed in full. The last value is small enough for scientific notation, where Exp => 3 produces a three-digit exponent.

$ gnatmake -gnat2022 -gnatwa floats.adb
$ ./floats
3.14
3.14159
12345.68
1.23E-04

An aligned table

A report combines a fixed-width text column with numeric columns. Because the name is stored in a String (1 .. 10), it already has a constant width, and the integer and price are padded by their Width and Fore fields. The total is accumulated in a Float and printed with the same price format, so the decimal points of every row and the total sit in one column.

table.adb
with Ada.Text_IO; use Ada.Text_IO;

procedure Table is
   package Int_IO is new Ada.Text_IO.Integer_IO (Integer);
   package Price_IO is new Ada.Text_IO.Float_IO (Float);

   type Item is record
      Name  : String (1 .. 10);
      Qty   : Integer;
      Price : Float;
   end record;

   Items : constant array (1 .. 4) of Item :=
     [("Ada book  ", 2, 39.90),
      ("GNAT guide", 1, 24.50),
      ("SPARK book", 3, 45.00),
      ("Ebook     ", 10, 9.99)];

   Total : Float := 0.0;
begin
   Put_Line ("Item         Qty     Price");
   Put_Line ("--------------------------");
   for I of Items loop
      Put (I.Name);
      Int_IO.Put (I.Qty, Width => 5);
      Price_IO.Put (I.Price, Fore => 7, Aft => 2, Exp => 0);
      New_Line;
      Total := Total + Float (I.Qty) * I.Price;
   end loop;
   Put_Line ("--------------------------");
   Put ("Total          ");
   Price_IO.Put (Total, Fore => 7, Aft => 2, Exp => 0);
   New_Line;
end Table;

The quantity column is five characters wide and the price column seven, which includes the decimal point and the two digits after it. The names are padded with spaces inside the literals so that the aggregate has one uniform type. The total is 2 * 39.90 + 1 * 24.50 + 3 * 45.00 + 10 * 9.99, or 339.20, and it lands under the price column because the label before it has the same length as a name plus a quantity field.

$ gnatmake -gnat2022 -gnatwa table.adb
$ ./table
Item         Qty     Price
--------------------------
Ada book      2     39.90
GNAT guide    1     24.50
SPARK book    3     45.00
Ebook        10      9.99
--------------------------
Total              339.20

Roman numerals

Not every representation comes from a library. Roman numerals use seven symbols and six subtractive pairs, and the greedy rule subtracts the largest symbol that still fits. The program keeps the values and their symbols in two parallel constant arrays, walks them from the largest to the smallest, and appends a symbol as often as it fits. The result is built in an Unbounded_String because its final length is not known in advance.

roman.adb
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;

procedure Roman is
   Values : constant array (1 .. 13) of Positive :=
     [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
   Names  : constant array (1 .. 13) of Unbounded_String :=
     [To_Unbounded_String ("M"),  To_Unbounded_String ("CM"),
      To_Unbounded_String ("D"),  To_Unbounded_String ("CD"),
      To_Unbounded_String ("C"),  To_Unbounded_String ("XC"),
      To_Unbounded_String ("L"),  To_Unbounded_String ("XL"),
      To_Unbounded_String ("X"),  To_Unbounded_String ("IX"),
      To_Unbounded_String ("V"),  To_Unbounded_String ("IV"),
      To_Unbounded_String ("I")];

   function To_Roman (N : Positive) return String is
      Result : Unbounded_String;
      Left   : Natural := N;
   begin
      for I in Values'Range loop
         while Left >= Values (I) loop
            Append (Result, Names (I));
            Left := Left - Values (I);
         end loop;
      end loop;
      return To_String (Result);
   end To_Roman;
begin
   for N in 1 .. 12 loop
      Put (To_Roman (N));
      Put (" ");
   end loop;
   New_Line;
   Put_Line ("1994 = " & To_Roman (1994));
   Put_Line ("2026 = " & To_Roman (2026));
end Roman;

The twelve small numbers exercise every symbol up to XII, and 1994 shows three subtractive pairs: CM for 900, XC for 90, and IV for 4, after M for 1000. The table produces the conventional forms for 1 to 3999; a value of 4000 or more would need an overbar convention that this simple program does not implement.

$ gnatmake -gnat2022 -gnatwa roman.adb
$ ./roman
I II III IV V VI VII VIII IX X XI XII
1994 = MCMXCIV
2026 = MMXXVI

Next steps

The next article moves from output formatting to the numeric types behind prices and measurements. It declares binary and decimal fixed-point types, shows how a value is rounded to the type's small, keeps money in exact cents, and prints amounts with currency pictures. The text output article covers the same Integer_IO and Float_IO packages from the general side, and attributes explains 'Image for the other scalar types.

Source

Ada Reference Manual: The Package Text_IO.Editing

Ada Reference Manual: The Package Text_IO

GNAT User's Guide

In this article we have removed the leading space from 'Image, printed integers in binary, octal, and hexadecimal with a fixed width, controlled the integer and fractional parts of a float, aligned a small price table, and converted numbers to Roman numerals. The strings article explains the trimming used in the first section.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Ada tutorials.