Ada & SPARK DISCUSSION

Why does Integer'Image add a leading space in Ada, and how do I format numbers cleanly?

Started by mohsinali Ada Integer'ImagePut_Line integerAda.Text_IO formattingTrimFloat_Text_IO
4 replies 248 views 5 participants
Latest activity · 30 Sep 2026

Why does Integer'Image add a leading space in Ada, and how do I format numbers cleanly?

mohsinali Ada & SPARK Forum
#1

I print a reading with Put_Line ("Temp=" & Integer'Image (T) & "C"); and get Temp= 25C, with an unwanted space after the equals sign. When I use Ada.Integer_Text_IO.Put (T); instead, the number appears after a long run of spaces. And Put_Line (T); does not compile at all.

Where does the space come from, and what is the normal way to print integers and floating-point values in a fixed, compact format, for example for a log line sent over a serial port?

Community replies 4

Re: Why does Integer'Image add a leading space in Ada, and how do I format numbers cleanly?

#2

Integer'Image returns the decimal digits preceded by a minus sign for negative values and by a single space for non-negative ones, so that the sign position is always present: Integer'Image (25) is " 25", three characters, and Integer'Image (-25) is "-25". The space is defined by the language; it is not a compiler quirk.

Put_Line (T) fails because Ada.Text_IO.Put_Line only accepts a String, and there is no implicit conversion from Integer.

Re: Why does Integer'Image add a leading space in Ada, and how do I format numbers cleanly?

#3

The usual fix is to trim. With Ada.Strings.Fixed visible, Trim (Integer'Image (T), Ada.Strings.Left) gives "25" and leaves "-25" alone. Wrap it in a small function, for example function Img (N : Integer) return String, and use that everywhere.

Slicing off the first character with S (S'First + 1 .. S'Last) also works, but only for non-negative values, since for a negative number the first character is the minus sign. Recent compilers also accept the attribute on an object, T'Image, which saves naming the type but returns the same string, leading space included.

Re: Why does Integer'Image add a leading space in Ada, and how do I format numbers cleanly?

#4

The long run of spaces from Ada.Integer_Text_IO.Put comes from its default field width, which is Integer'Width: 11 for a 32-bit Integer, enough for -2147483648. The value is right-justified in that field, so 25 is preceded by nine spaces.

Pass the width explicitly: Put (T, Width => 0) prints just the digits, and Put (T, Width => 5) gives right-aligned columns, which is handy for tables.

Re: Why does Integer'Image add a leading space in Ada, and how do I format numbers cleanly?

#5

For floating point, Float'Image (3.14) gives exponent notation such as " 3.14000E+00", which is rarely what you want in a log. Use Ada.Float_Text_IO.Put (X, Fore => 1, Aft => 2, Exp => 0) to get 3.14: Fore is the minimum number of characters before the point, Aft the digits after it, and Exp => 0 selects plain decimal notation.

There is also a Put that writes into a String instead of a file, useful when you are assembling a message for a serial driver. For your own numeric types, instantiate Ada.Text_IO.Integer_IO, Float_IO or Fixed_IO with the type.

TEP COMMUNITY