Figure 20.
Sales Tax.
----------------------------------------------------------
-- This program shows how to use the MONEY package.
with MONEY, ASCII_UTILITIES, STANDARD_INTEGERS;
with TEXT_IO; use TEXT_IO;
procedure Sales_Tax is
PRICE, TAX, COST : MONEY.Cents;
RATE : float;
TEXT : string(1..79);
LENGTH : natural;
function "*"(LEFT : MONEY.Cents; RIGHT : float)
return MONEY.Cents is
use STANDARD_INTEGERS;
X : float;
RESULT : Integer_32;
begin
-- Compute exact amount
X := float(MONEY.Dimensionless(LEFT)) * RIGHT;
-- Round to the nearest cent
RESULT := Integer_32(X);
-- Convert the answer to Cents
return MONEY.Type_Convert(RESULT);
end "*";
use MONEY; -- for "+" operator in COST := PRICE + TAX;
begin
put("What is the cost of the item? ");
get_line(TEXT,LENGTH); new_line;
PRICE := MONEY.Value(TEXT(1..LENGTH));
put("What is the sales tax rate? " );
get_line(TEXT,LENGTH); new_line;
RATE := ASCII_UTILITIES.Value(TEXT(1..LENGTH));
-- if RATE > 1, the user must have entered
-- a percentage (i.e. 6%) instead of 0.06.
if RATE > 1.0 then RATE := RATE / 100.0; end if;
TAX := PRICE * RATE;
declare
TAX_STRING : string(1..MONEY.Width(TAX));
begin
TAX_STRING := MONEY.Image(TAX);
put_line("The tax on that item is "
& TAX_STRING);
end;
COST := PRICE + TAX;
put("Your total cost is ");
put(MONEY.Image(COST));
put_line(".");
end Sales_Tax;