Ada 95 Quality and Style Guide Chapter 8

Chapter 8: Reusability - TOC - 8.3 ADAPTABILITY

8.3.7 Decimal Type Output and Information Systems Annex

guideline

  • Localize the currency symbol, digits separator, radix mark, and fill character in picture output.
  • Consider using the # character in picture layouts so that the edited numeric output lengths are invariant across currency symbols of different lengths.

  • example

    with Ada.Text_IO.Editing;
    package Currency is
    
       type Dollars is delta 0.01 digits 10;
       type Marks   is delta 0.01 digits 10;
    
       package Dollar_Output is
          new Ada.Text_IO.Editing.Decimal_Output
                 (Num                => Dollars,
                  Default_Currency   => "$",
                  Default_Fill       => '*',
                  Default_Separator  => ',',
                  Default_Radix_Mark => '.');
    
       package Mark_Output is
          new Ada.Text_IO.Editing.Decimal_Output
                 (Num                => Marks,
                  Default_Currency   => "DM",
                  Default_Fill       => '*',
                  Default_Separator  => '.',
                  Default_Radix_Mark => ',');
    
    end Currency;
    with Ada.Text_IO.Editing;
    with Currency;  use Currency;
    procedure Test_Picture_Editing is
    
       DM_Amount     : Marks;
       Dollar_Amount : Dollars;
    
       Amount_Picture : constant Ada.Text_IO.Editing.Picture 
          := Ada.Text_IO.Editing.To_Picture ("##ZZ_ZZZ_ZZ9.99");
    
    begin   -- Test_Picture_Editing
    
       DM_Amount     := 1_234_567.89;
       Dollar_Amount := 1_234_567.89;
    
       DM_Output.Put (Item => DM_Amount,
                      Pic  => Amount_Picture);
    
       Dollar_Output.Put (Item => Dollar_Amount,
                          Pic  => Amount_Picture);
       
    end Test_Picture_Editing;
    
    

    rationale

    Currencies differ in how they are displayed in a report. Currencies use different symbols of different lengths (e.g., the American $, the German DM, and the Austrian ÖS). They use different symbols to separate digits. The United States and the United Kingdom use the comma to separate groups of thousands, whereas Continental Europe uses the period. The United States and the United Kingdom use a period as a decimal point; Continental Europe uses the comma. For a program involving financial calculations that is to be reused across countries, you need to take these differences into account. By encapsulating them, you limit the impact of change in adapting the financial package.


    < Previous Page Search Contents Index Next Page >
    1 2 3 4 5 6 7 8 9 10 11
    TOC TOC TOC TOC TOC TOC TOC TOC TOC TOC TOC
    Appendix References Bibliography