ada Outputting numbers Print Decimal Fixed Point Numbers, aka Money

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Ada.Text_IO.Editing offers formatting decimal fixed point values using “picture strings”. These describe output using “magical” characters for separators, currency signs, etc.

with Ada.Text_IO.Editing;   use Ada.Text_IO;

procedure Print_Value is

    Max_Count      : constant := 1_000_000;

    type Fruit is (Banana, Orange, Pear);
    subtype Count is Integer range -Max_Count .. +Max_Count;

    type Money is delta 0.001 digits 10;

    package Fruit_IO is new Enumeration_IO (Fruit);
    package Money_IO is new Editing.Decimal_Output
      (Money,
       Default_Currency => "CHF",
       Default_Separator => ''');

    Inventory : constant array (Fruit) of Count :=
      (Banana => +27_420,
       Orange => +140_600,
       Pear   => -10_000);

    Price_List : constant array (Fruit) of Money :=
      (Banana => 0.07,
       Orange => 0.085,
       Pear   => 0.21);

    Format : constant Editing.Picture :=
      Editing.To_Picture ("<###BZ_ZZZ_ZZ9.99>");
begin
    Fruit_IO.Default_Width := 12;

    for F in Inventory'Range loop
        Fruit_IO.Put (F);
        Put          (" | ");
        Money_IO.Put (Item => Inventory (F) * Price_List (F),
                      Pic => Format);
        New_Line;
    end loop;
end Print_Value;

Result

BANANA       |  CHF     1'919.40 
ORANGE       |  CHF    11'951.00 
PEAR         | (CHF     2'100.00)


Got any ada Question?