Skip to content

Commit

Permalink
Updated formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
AJ-Ianozi committed Aug 5, 2023
1 parent e40d90a commit d142893
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 71 deletions.
8 changes: 6 additions & 2 deletions alire.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name = "cashe"
description = "A money library written in Ada."
version = "0.1.0-dev"
description = "A fixed-point decimal money library written in Ada."
version = "1.0.0"
licenses = "MIT"

website = "https://github.com/AJ-Ianozi/Cashe/"
tags = [ "currency", "money", "decimal", "finance" ]

authors = ["AJ Ianozi"]
maintainers = ["AJ Ianozi <aj@ianozi.com>"]
Expand Down
5 changes: 2 additions & 3 deletions src/cashe-exchange.adb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pragma Ada_2022;
with Ada.Strings.Wide_Wide_Hash;
with Ada.Text_IO; use Ada.Text_IO;
package body Cashe.Exchange is

-- TODO: looks promising: https://github.com/fawazahmed0/currency-api and
Expand Down Expand Up @@ -302,8 +301,8 @@ package body Cashe.Exchange is
-- These can be used if the base is enabled.
procedure Set_Rate
(This : in out Currency_Exchange;
To : Currency_Handling.Custom_Currency;
Rate : Decimal)
To : Currency_Handling.Custom_Currency;
Rate : Decimal)
is
use Currency_Handling;
begin
Expand Down
3 changes: 1 addition & 2 deletions src/cashe-exchange.ads
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ package Cashe.Exchange is
-- Bitcoin : Custom_Currency :=
-- Create (Code => "BTC", Minor_Unit => 8,
-- Name => "Bitcoin", Symbol => "฿");
-- -- based on the Jul. 9, 2023 exchange rate
-- -- based on the Jul. 9, 2023 exchange rate
-- -- from openexchangerates.org.
-- BTC_to_USD : constant Decimal := 30196.620159;
-- USD_to_BTC : constant Decimal := 0.0000331163;
Expand Down Expand Up @@ -413,7 +413,6 @@ package Cashe.Exchange is
-- The currency to convert from
To : ISO.Currencies.Currency)
-- The currency to convert to

return Decimal;
function Rate
(This : Currency_Exchange;
Expand Down
59 changes: 29 additions & 30 deletions src/cashe-money_handling.adb
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ package body Cashe.Money_Handling is
By : Natural;
Method : Round_Method := Half_Even)
return Money
is ( (Amount => Round (This.Amount, By, Method),
Cur => This.Cur));
is ((Amount => Round (This.Amount, By, Method), Cur => This.Cur));
function Round (This : Money; Method : Round_Method := Half_Even)
return Money is
(This.Round (By => This.Currency_Unit, Method => Method));
Expand All @@ -84,39 +83,39 @@ package body Cashe.Money_Handling is
end As_Minor;

-- Operator overloading
function "+" (Left, Right : Money) return Money is
(if Left.Same_Currency (Right) then
(Amount => Left.Amount + Right.Amount, Cur => Left.Cur)
else raise Currency_Mismatch);
function "+" (Left : Money; Right : Decimal) return Money is
((Amount => Left.Amount + Right, Cur => Left.Cur));
function "+" (Left : Money; Right : Decimal_Minor) return Money is
(Left + Decimal (Right));
function "+" (Left, Right : Money) return Money is
(if Left.Same_Currency (Right) then
(Amount => Left.Amount + Right.Amount, Cur => Left.Cur)
else raise Currency_Mismatch);
function "+" (Left : Money; Right : Decimal) return Money is
((Amount => Left.Amount + Right, Cur => Left.Cur));
function "+" (Left : Money; Right : Decimal_Minor) return Money is
(Left + Decimal (Right));

function "-" (Left : Money) return Money is
((Amount => -(Left.Amount), Cur => Left.Cur));
function "-" (Left, Right : Money) return Money is
(if Left.Same_Currency (Right) then
(Amount => Left.Amount - Right.Amount, Cur => Left.Cur)
else raise Currency_Mismatch);
function "-" (Left : Money; Right : Decimal) return Money is
((Amount => Left.Amount - Right, Cur => Left.Cur));
function "-" (Left : Money; Right : Decimal_Minor) return Money is
(Left - Decimal (Right));
function "-" (Left : Money) return Money is
((Amount => -(Left.Amount), Cur => Left.Cur));
function "-" (Left, Right : Money) return Money is
(if Left.Same_Currency (Right) then
(Amount => Left.Amount - Right.Amount, Cur => Left.Cur)
else raise Currency_Mismatch);
function "-" (Left : Money; Right : Decimal) return Money is
((Amount => Left.Amount - Right, Cur => Left.Cur));
function "-" (Left : Money; Right : Decimal_Minor) return Money is
(Left - Decimal (Right));

function "*" (Left, Right : Money) return Money is
(if Left.Same_Currency (Right) then
(Amount => Left.Amount * Right.Amount, Cur => Left.Cur)
else raise Currency_Mismatch);
function "*" (Left : Money; Right : Decimal) return Money is
((Amount => Left.Amount * Right, Cur => Left.Cur));
function "*" (Left : Money; Right : Decimal_Minor) return Money is
(Left * Decimal (Right));
function "*" (Left, Right : Money) return Money is
(if Left.Same_Currency (Right) then
(Amount => Left.Amount * Right.Amount, Cur => Left.Cur)
else raise Currency_Mismatch);
function "*" (Left : Money; Right : Decimal) return Money is
((Amount => Left.Amount * Right, Cur => Left.Cur));
function "*" (Left : Money; Right : Decimal_Minor) return Money is
(Left * Decimal (Right));

function "/" (Left, Right : Money) return Money is
(if Left.Same_Currency (Right) then
(if (not Right.Is_Zero) then
(Amount => Left.Amount / Right.Amount, Cur => Left.Cur)
(if not Right.Is_Zero then
(Amount => Left.Amount / Right.Amount, Cur => Left.Cur)
else raise Division_By_Zero)
else raise Currency_Mismatch);
function "/" (Left : Money; Right : Decimal) return Money is
Expand Down
58 changes: 28 additions & 30 deletions src/cashe-money_handling.ads
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pragma Assertion_Policy (Check);
with ISO.Currencies;
with Ada.Strings.Text_Buffers;
with Cashe.Currency_Handling;
with Ada.Exceptions;
-- ****h* Cashe/Money_Handling
-- SOURCE
package Cashe.Money_Handling is
Expand Down Expand Up @@ -31,7 +30,7 @@ package Cashe.Money_Handling is
-- SOURCE
type Money is tagged private with Put_Image => Print_Money;
-- DESCRIPTION
-- An immutable datatype that can be created and stored
-- An immutable datatype that can be created and stored
-- (or just created on the spot) using various combinations of
-- From_Major and From_Minor.
-- USAGE
Expand Down Expand Up @@ -66,7 +65,7 @@ package Cashe.Money_Handling is
-- * Money_Handling.Money/As_Minor
-- SEE ALSO
-- * Money_Handling/Money_Handling.From_Major
-- * Money_Handling/Money_Handling.From_Minor
-- * Money_Handling/Money_Handling.From_Minor
-- ****

-- ****m* Money_Handling.Money/Same_Currency
Expand All @@ -87,7 +86,7 @@ package Cashe.Money_Handling is
-- they are different.
-- EXAMPLE
-- if My_USD.Same_Currency (My_USD2) then
-- Put_Line ("Same Currency.");
-- Put_Line ("Same Currency.");
-- end if;
-- ****

Expand All @@ -100,7 +99,7 @@ package Cashe.Money_Handling is
-- Boolean - True if currency is custom or not, as opposed to an ISO's.
-- EXAMPLE
-- if My_Money.Custom_Currency then
-- Put_Line ("Using a custom currency.");
-- Put_Line ("Using a custom currency.");
-- end if;
-- ****

Expand Down Expand Up @@ -191,7 +190,7 @@ package Cashe.Money_Handling is
-- RETURN VALUE
-- Boolean:
-- * True if value of money is 0.
-- * False if value is not 0.
-- * False if value is not 0.
-- EXAMPLE
-- if My_Money.Is_Zero then
-- Put_Line ("I'm broke!");
Expand All @@ -206,7 +205,7 @@ package Cashe.Money_Handling is
-- RETURN VALUE
-- Boolean:
-- * True if value of money is greater than 0.
-- * False if value is not greater than 0.
-- * False if value is not greater than 0.
-- EXAMPLE
-- if My_Money.Is_Positive then
-- Put_Line ("I have money!");
Expand Down Expand Up @@ -306,7 +305,6 @@ package Cashe.Money_Handling is
-- Money_Handling.Money/Full_Precision
-- ****


-- Returns the item in minor format, rounded with banker's rounding.
-- If money holds values "2000.005, return "200000"
-- ****m* Money_Handling.Money/As_Minor
Expand Down Expand Up @@ -342,13 +340,13 @@ package Cashe.Money_Handling is
-- Money_Handling/Money_Handling.Money - Sum of the addition.
-- EXAMPLE
-- declare
-- USD1 : Money := From_Major (7.22, "USD") + From_Major (0.98, "USD");
-- USD2 : Money := USD1 + 10;
-- USD3 : Money := USD2 + 12.34;
-- USD1 : Money := From_Major (7.22, "USD") + From_Major (0.98, "USD");
-- USD2 : Money := USD1 + 10;
-- USD3 : Money := USD2 + 12.34;
-- begin
-- Put_Line (USD1'Image); -- $ 15.20
-- Put_Line (USD2'Image); -- $ 25.20
-- Put_Line (USD3'Image); -- $ 37.54
-- Put_Line (USD1'Image); -- $ 15.20
-- Put_Line (USD2'Image); -- $ 25.20
-- Put_Line (USD3'Image); -- $ 37.54
-- end;
-- ****

Expand Down Expand Up @@ -393,13 +391,13 @@ package Cashe.Money_Handling is
-- Money_Handling/Money_Handling.Money - Product of the multiplication.
-- EXAMPLE
-- declare
-- USD1 : Money := From_Major (7.20, "USD") * From_Major (1.90, "USD");
-- USD2 : Money := USD1 * 10;
-- USD3 : Money := USD2 * 12.34;
-- USD1 : Money := From_Major (7.20, "USD") * From_Major (1.90, "USD");
-- USD2 : Money := USD1 * 10;
-- USD3 : Money := USD2 * 12.34;
-- begin
-- Put_Line (USD1'Image); -- $ 13.68
-- Put_Line (USD2'Image); -- $ 136.8
-- Put_Line (USD3'Image); -- $ 1688.11
-- Put_Line (USD1'Image); -- $ 13.68
-- Put_Line (USD2'Image); -- $ 136.8
-- Put_Line (USD3'Image); -- $ 1688.11
-- end;
-- ****

Expand All @@ -422,13 +420,13 @@ package Cashe.Money_Handling is
-- Money_Handling/Money_Handling.Money - Quotient of the division.
-- EXAMPLE
-- declare
-- USD1 : Money := From_Major (120, "USD") / From_Major (15.00, "USD");
-- USD2 : Money := USD1 / 10;
-- USD3 : Money := USD2 / 0.25;
-- USD1 : Money := From_Major (120, "USD") / From_Major (15.00, "USD");
-- USD2 : Money := USD1 / 10;
-- USD3 : Money := USD2 / 0.25;
-- begin
-- Put_Line (USD1'Image); -- $ 8.00
-- Put_Line (USD2'Image); -- $ 0.80
-- Put_Line (USD3'Image); -- $ 3.20
-- Put_Line (USD1'Image); -- $ 8.00
-- Put_Line (USD2'Image); -- $ 0.80
-- Put_Line (USD3'Image); -- $ 3.20
-- end;
-- ****

Expand Down Expand Up @@ -462,7 +460,7 @@ package Cashe.Money_Handling is
-- ERRORS
-- * Money_Handling/Money_Handling.Currency_Mismatch
-- RETURN VALUE
-- Boolean: Money < X
-- Boolean: Money < X
-- EXAMPLE
-- declare
-- My_Money : Money := From_Major (875.00, "USD");
Expand All @@ -489,7 +487,7 @@ package Cashe.Money_Handling is
-- ERRORS
-- * Money_Handling/Money_Handling.Currency_Mismatch
-- RETURN VALUE
-- Boolean: Money > X
-- Boolean: Money > X
-- EXAMPLE
-- declare
-- My_Money : Money := From_Major (875.00, "USD");
Expand All @@ -516,7 +514,7 @@ package Cashe.Money_Handling is
-- ERRORS
-- * Money_Handling/Money_Handling.Currency_Mismatch
-- RETURN VALUE
-- Boolean: Money <= X
-- Boolean: Money <= X
-- EXAMPLE
-- declare
-- My_Money : Money := From_Major (875.00, "USD");
Expand Down Expand Up @@ -544,7 +542,7 @@ package Cashe.Money_Handling is
-- ERRORS
-- * Money_Handling/Money_Handling.Currency_Mismatch
-- RETURN VALUE
-- Boolean: Money >= X
-- Boolean: Money >= X
-- EXAMPLE
-- declare
-- My_Money : Money := From_Major (875.00, "USD");
Expand Down
2 changes: 0 additions & 2 deletions src/cashe.adb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ package body Cashe is
end if;
end To_Decimal;


function To_Decimal
(Item : Long_Long_Float; Precision : Natural := 20)
return Decimal is
Expand Down Expand Up @@ -257,7 +256,6 @@ package body Cashe is
end case;
end To_Decimal;


function To_Decimal
(Item : Long_Float; Precision : Natural := 20)
return Decimal is
Expand Down
4 changes: 2 additions & 2 deletions src/cashe.ads
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package Cashe is
-- DESCRIPTION
-- This package provides datatypes and functions utilized by other packages.
-- ****

Version : constant String := "1.0.0";
-- ****t* Cashe/Cashe.Decimal
-- SOURCE
type Decimal is delta 1.0E-20 digits 38;
Expand Down Expand Up @@ -106,7 +106,7 @@ package Cashe is
pre => Precision <= Max_Precision;
-- FUNCTION
-- Convert a floating point number or minor unit to a Decimal based on
-- precision. Highly recommended to use this function with
-- precision. Highly recommended to use this function with
-- Long_Long_Float for highest precision.
-- PARAMETERS
-- Item - Floating point to be converted.
Expand Down

0 comments on commit d142893

Please sign in to comment.