列印十進位制定點數也稱錢
Ada.Text_IO.Editing
使用圖片字串提供格式化十進位制定點值。這些描述輸出使用魔法字元分隔符,貨幣符號等。
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;
結果
BANANA | CHF 1'919.40
ORANGE | CHF 11'951.00
PEAR | (CHF 2'100.00)