San Jose State University Department of Applied Data Science
**DATA 200 Computational Programming for Data Analytics**
Spring 2023 Instructor: Ron Mak
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3.14 Using Type Decimal for Monetary Amounts"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Floating-point numbers can suffer from roundoff errors. Financial applications can require precise \"to the penny\" calculations. The Python Standard Library provides datatype `Decimal` that uses a fair rounding algorithm."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"amount = 112.31"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(amount)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(f'{amount:.20f}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Importing Type Decimal from the decimal Module "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### The Python Standard Library is organized into **modules**. One module is `decimal` from which we can import the datatype `Decimal`. If we use any functions or datatypes from a module, we must first import them."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from decimal import Decimal"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Creating Decimals\n",
"\n",
"#### Create a `Decimal` number from a string."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"principal = Decimal('1000.00')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"principal"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"rate = Decimal('0.05')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"rate"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Decimal Arithmetic "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = Decimal('10.5')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y = Decimal('2')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x + y"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x // y"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x += y"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### You can perform arithmetic operations between a `Decimal` value and an integer value, but not between a `Decimal` value and a floating-point value."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Calculating Compound Interest"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### A person puts $1,000 in an investment that yields 5% interest per year. The person leaves all the interest on deposit in the investment. What is the amount of the investment after 10 years?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for year in range(1, 11):\n",
" amount = principal*((1 + rate)**year)\n",
" print(f'{year:>2}:{amount:>10.2f}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Note that the print format specifiers each has a `>` sign. That specifies the output to be right aligned within its field width (the number of spaces to print the value)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### **EXERCISE:** Assume that the tax on a restaurant bill is 6.25% and that the bill amount is $37.45. Use datatype `Decimal` to calculate the bill total then print the result with two digits to the right of the decimal point."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"##########################################################################\n",
"# (C) Copyright 2019 by Deitel & Associates, Inc. and #\n",
"# Pearson Education, Inc. All Rights Reserved. #\n",
"# #\n",
"# DISCLAIMER: The authors and publisher of this book have used their #\n",
"# best efforts in preparing the book. These efforts include the #\n",
"# development, research, and testing of the theories and programs #\n",
"# to determine their effectiveness. The authors and publisher make #\n",
"# no warranty of any kind, expressed or implied, with regard to these #\n",
"# programs or to the documentation contained in these books. The authors #\n",
"# and publisher shall not be liable in any event for incidental or #\n",
"# consequential damages in connection with, or arising out of, the #\n",
"# furnishing, performance, or use of these programs. #\n",
"##########################################################################\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Additional material (C) Copyright 2023 by Ronald Mak"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 4
}