Currency: Difference between revisions

1,503 bytes added ,  3 years ago
Added Ada
(Added an example based on the Bankster library)
(Added Ada)
Line 36:
Dollar signs and thousands separators are optional.
<br><br>
 
=={{header|Ada}}==
Numeric constants in Ada do not have to be given an explicit type. When used in constant expressions together, they have
arbitrary precision. This enables simple calculation of the values. Ada also has builtin fixed-point
types, which can define a minimum interval between decimal values (in this case, 1 cent or $0.01).
 
When using the Dollar_IO package to print each value, the constant is converted into a Dollar, then printed. All of
these arbitrary-precision arithmetic operations are done at compile-time, incurring no runtime cost.
<lang ada>with Ada.Text_IO;
 
procedure Currency is
type Dollar is delta 0.01 range 0.0 .. 24_000_000_000_000_000.0;
package Dollar_IO is new Ada.Text_IO.Fixed_IO(Dollar);
 
hamburger_cost : constant := 5.50;
milkshake_cost : constant := 2.86;
tax_rate : constant := 0.0765;
 
total_cost : constant := hamburger_cost * 4_000_000_000_000_000.0 + milkshake_cost * 2;
total_tax : constant := total_cost * tax_rate;
total_with_tax : constant := total_cost + total_tax;
begin
Ada.Text_IO.Put("Price before tax:");
Dollar_IO.Put(total_cost);
Ada.Text_IO.New_Line;
 
Ada.Text_IO.Put("Tax: ");
Dollar_IO.Put(total_tax);
Ada.Text_IO.New_Line;
 
Ada.Text_IO.Put("Total: ");
Dollar_IO.Put(total_with_tax);
Ada.Text_IO.New_Line;
end Currency;</lang>
{{out}}
<pre>
Price before tax: 22000000000000005.72
Tax: 1683000000000000.44
Total: 23683000000000006.16
</pre>
 
=={{header|ALGOL 68}}==
6

edits