Data update

This commit is contained in:
Ingy döt Net 2026-04-30 12:34:36 -04:00
parent 4bb20c9b71
commit cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions

View file

@ -0,0 +1,26 @@
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;

View file

@ -0,0 +1,28 @@
>>SOURCE FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. currency-example.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Burger-Price CONSTANT 5.50.
01 Milkshake-Price CONSTANT 2.86.
01 num-burgers PIC 9(18) VALUE 4000000000000000.
01 num-milkshakes PIC 9(18) VALUE 2.
01 tax PIC 9(18)V99.
01 tax-edited PIC $(17)9.99.
01 Tax-Rate CONSTANT 7.65.
01 total PIC 9(18)V99.
01 total-edited PIC $(17)9.99.
PROCEDURE DIVISION.
COMPUTE total rounded, total-edited rounded =
num-burgers * Burger-Price + num-milkshakes * Milkshake-Price
DISPLAY "Total before tax: " total-edited
COMPUTE tax rounded, tax-edited rounded = total * (Tax-Rate / 100)
DISPLAY " Tax: " tax-edited
ADD tax TO total GIVING total-edited rounded
DISPLAY " Total with tax: " total-edited
.
END PROGRAM currency-example.

View file

@ -0,0 +1,28 @@
require "big"
def f (n, d=2); n.format(decimal_places: d) end
check = <<-EOC.lines.map(&.split).map {|(desc, price, qty)| { desc, price.to_big_d, qty.to_big_d } }
Hamburger 5.50 4000000000000000
Milkshake 2.86 2
EOC
tax_rate = 0.0765
fmt = "%-10s %8s %23s %27s\n"
printf fmt, %w(Item Price Quantity Amount)
subtotal = check.sum {|item, price, quant|
amount = price * quant
printf fmt, item, f(price), f(quant, 0), f(amount.round(2))
amount
}
printf fmt, "", "", "", "-------------------------"
printf fmt, "", "", "Subtotal ", f(subtotal)
tax = (subtotal * tax_rate).round(2)
printf fmt, "", "", "Tax ", f(tax)
total = subtotal + tax
printf fmt, "", "", "Total ", f(total)

View file

@ -0,0 +1,86 @@
!
program Currency
implicit none
! Store all money amounts in units of 1/10 Cent. (Cents only can result in rounding errors in Tax)
! Store Tax rate in PPM not in % for controlled, sufficient precision in integer calculation
!
! Use integer(kind=16) for internal represenation. This can carry up to 38 digits which
! gives a pretty amount of money, enough for most purposes.
! To display an amount V of money in terms of real xxx.yy $ rounded to nearest full cent,
! divide the V by moneyFactor to give the full Dollars, and
! calculate Cents as nearest Integer nint ( mod (V,moneyFactor) / CentFactor)
! Similarly, we represent Tax percentage in units of ppm, again using Integerr(kind=16)
! To obtain the Tax, multiply value V with TaxPPM, then divide by PPMFactor
! Define constants here to avoid magic numbers within code
integer, parameter :: int_kind = 16 ! Will not work with smaller int_kind
integer, parameter :: float_kind = 8
integer, parameter :: moneyFactor = 1000
real(kind=float_kind) :: CentFactor = 10.0
integer (kind=int_kind), parameter :: PPMFactor = 1000000 ! 1 ppm = 1/1000000
integer (kind=int_kind), parameter :: percentToPPM = 10000 ! 1 % = 10000 ppm
integer (kind=int_kind) :: N_Hamburgers
integer (kind=int_kind) :: N_Milkshake
integer (kind=int_kind) :: Price_Hamburger
integer (kind=int_kind) :: Price_Milkshake
integer (kind=int_kind) :: Tax_Rate_PPM
integer (kind=int_kind) :: Price_AllHamburgers, Price_AllMilkshakes, PriceBeforeTax, Tax, TotalWithTax
! Amounts, Prices and Tax Rate as in Task description
N_Hamburgers = 4000000000000000_int_kind ! pure number
N_Milkshake = 2 ! pure number
Price_Hamburger = nint (5.50 * MoneyFactor) ! $ 5.50 in 1/10 cent, see above
Price_Milkshake = nint(2.86*MoneyFactor) ! $2.86 in 1/10 cent
Tax_rate_PPM = nint (7.65*percentToPPM) ! 7.65% converted to ppm
! Very elementary math, additions and multiplication by integer values are exact unless overflow ocurs
Price_AllHamburgers = N_Hamburgers * Price_Hamburger
Price_AllMilkshakes = N_Milkshake * Price_Milkshake
PriceBeforeTax = Price_AllHamburgers + Price_AllMilkshakes
! Multiplication by (percentage value * percentToPPM (=10000))
! and subsequent division by PPMFactor (=1000000) cuts off
! less than 0.1 cents from the exact result
!
Tax = (Tax_rate_PPM * PriceBeforeTax) / PPMFactor
! Addition is exact again.
TotalWithTax = PriceBeforeTax + Tax
! Display the results rounded to closest Dollar amount values
! with 2 decimal digits
!
! Spacing selected to display the expected result.
! More general solution for pretty-print required.
!
call printAsDollars ('Hamburgers : $ ', Price_AllHamburgers)
call printAsDollars ('Milk Shakes : $ ', Price_AllMilkshakes)
call printAsDollars ('Total Price Before Tax: $ ', PriceBeforeTax)
call printAsDollars ('Tax : $ ', Tax)
call printAsDollars ('Total : $ ', TotalWithTax)
contains
subroutine printAsDollars (text, amount)
character (len=*), intent(in) :: text
integer (kind = int_kind) :: amount
integer (kind = int_kind) :: fullDollars, Cents
fullDollars = amount / moneyFactor
Cents = nint (mod (amount, moneyFactor)/CentFactor)
! rounding with nint() might result in exactly 100 cents, which is 1 Dollar.
if (Cents .eq. 100) then
fullDollars = fullDollars + 1
Cents = 0
endif
write (6,'(A, i0,".",i2.2)') text, fullDollars, Cents
end subroutine printAsDollars
end program Currency

View file

@ -0,0 +1,19 @@
# use banker's rounding
mode rounding = _round'halfeven
val hamburgers = 4000000000000000
val milkshakes = 2
val price_hb = 5.50
val price_ms = 2.86
val taxrate = 7.65
val pretax = hamburgers * price_hb +
milkshakes * price_ms
val tax = round(pretax * taxrate / 100, places=2)
val total = pretax + tax
writeln "before tax: $", pretax
writeln " tax: $", tax
writeln " total: $", total

View file

@ -0,0 +1,15 @@
require "bignum"
local fmt = require "fmt"
local hamburgers = bigdec.of("4000000000000000")
local milkshakes = bigdec.of(2)
local price1 = bigdec.of(5.5)
local price2 = bigdec.of(2.86)
local tax_pc = bigdec.of(0.0765)
local total_pre_tax = hamburgers * price1 + milkshakes * price2
local total_tax = tax_pc * total_pre_tax
local total_after_tax = total_pre_tax + total_tax
fmt.print("Total price before tax : %20s", total_pre_tax:tostring(2))
fmt.print("Tax : %20s", total_tax:tostring(2))
fmt.print("Total price after tax : %20s", total_after_tax:tostring(2))

View file

@ -0,0 +1,20 @@
Rebol [
title: "Rosetta code: Currency"
file: %Currency.r3
url: https://rosettacode.org/wiki/Currency
needs: 3.0.0
]
hamburgers: 4'000'000'000'000'000
hamburger-price: $5.50
milkshakes: 2
milkshake-price: $2.86
tax-rate: 7.65%
total-price: (hamburgers * hamburger-price) + (milkshakes * milkshake-price)
tax: total-price * tax-rate
total: total-price + tax
print ["Total price before tax :" round/to total-price $0.01]
print ["Tax :" round/to tax $0.01]
print ["Total price after tax :" round/to total $0.01]