# 3.14 Using Type Decimal for Monetary Amounts¶

- Python’s built-in floating-point numbers work well for most applications.
- Floating-point values are stored in binary format.
- Some floating-point values are represented only approximately when they’re converted to binary.

In [1]:

    amount = 112.31

In [2]:

    print(amount)

    112.31

- Print `amount` with 20 digits of precision to the right of the decimal point to see that the actual floating-point value in memory is not exactly `112.31`—it’s only an approximation:

In [3]:

    print(f'{amount:.20f}')

    112.31000000000000227374

- The Python Standard Library provides many predefined capabilities you can use in your Python code to avoid “reinventing the wheel.”
- Type `Decimal`, which uses a special coding scheme to solve the problem of to-the-penny precision.
  - Banks also have to deal with issues like using a fair rounding algorithm when calculating daily interest on accounts—type `Decimal` offers such capabilities.

### Importing Type Decimal from the decimal Module¶

- The Python Standard Library is divided into groups of related capabilities called modules.
- The `decimal` module defines type `Decimal` and its capabilities.
- Must `import` to use capabilities from a module.

In [4]:

    from decimal import Decimal

### Creating Decimals¶

- Typically create a `Decimal` from a string.

In [5]:

    principal = Decimal('1000.00')

In [6]:

    principal

Out[6]:

    Decimal('1000.00')

In [7]:

    rate = Decimal('0.05')

In [8]:

    rate

Out[8]:

    Decimal('0.05')

### Decimal Arithmetic¶

- `Decimal`s support the standard arithmetic operators and augmented assignments.

In [9]:

    x = Decimal('10.5')

In [10]:

    y = Decimal('2')

In [11]:

    x + y

Out[11]:

    Decimal('12.5')

In [12]:

    x // y

Out[12]:

    Decimal('5')

In [13]:

    x += y

In [14]:

    x

Out[14]:

    Decimal('12.5')

- May perform arithmetic between `Decimal`s and integers, but not between `Decimal`s and floating-point numbers.

### Compound-Interest Problem Requirements Statement¶

Requirements statement:

> A person invests $1000 in a savings account yielding 5% interest. Assuming that the person leaves all interest on deposit in the account, calculate and display the amount of money in the account at the end of each year for 10 years. Use the following formula for determining these amounts:

> > a = p(1 + r)n

> where

> > p is the original amount invested (i.e., the principal),

> > r is the annual interest rate,

> > n is the number of years and

> > a is the amount on deposit at the end of the n th year.

### Calculating Compound Interest¶

- For each year, the loop displays a formatted string containing the year number and the amount on deposit at the end of that year

In [15]:

    for year in range(1, 11):
        amount = principal * (1 + rate) ** year
        print(f'{year:>2}{amount:>10.2f}')

     1   1050.00
     2   1102.50
     3   1157.62
     4   1215.51
     5   1276.28
     6   1340.10
     7   1407.10
     8   1477.46
     9   1551.33
    10   1628.89

### Formatting the Year and Amount on Deposit¶

    print(f'{year:>2}{amount:>10.2f}')

- Uses an f-string with two placeholders to format the loop’s output.

    {year:>2}

- Uses the format specifier `>2` to indicate that `year`’s value should be right aligned (`>`) in a field of width `2`
- The field width specifies the number of character positions to use when displaying the value.

- Can left align values with <.

    {amount:>10.2f}

- Formats `amount` as a floating-point number (`f`) right aligned (`>`) in a field width of `10` with a decimal point and two digits to the right of the decimal point (`.2`).

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 3 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.