0

I have been writing this program to add up money in a cash box. when I started multiplying I realized I needed to make a change in my code. int(1) * float(0.05) gave me 0.050000000000000003. I tried searching how to round it and I read that float will not accurately represent currency, I also found information about decimal and quatitize (). I tried to understand how to incorporate that into my code and I am struggling. how can accurately represent currency in my code?

money = {'nickles' : 0, 'dimes' : 0, 'quarters': 0, 'ones': 0, 'twos': 0, 'tens' : 0, 'twenties' : 0, 'fifties': 0, 'one hundreds' : 0}

def display_values():
    print(money['nickles'], 'nickles = ', money['nickles'] * 0.05, 'cents')

def assign_all_values():
    input_nickles()
    input_dimes()
    input_quarters()
    input_ones()
    input_twos()
    input_fives()
    input_tens()
    input_fifties()
    input_one_hundreds()

def input_nickles():    
    money['nickles'] = float(input('how many nickels are in your cash box? '))
    display_values()

def input_dimes():
    money['Dimes'] = float(input('how many dimes are in your cash box? '))
    display_values()

def input_quarters():
    money['Quarters'] = float(input('how many quarters are in your cash box? '))
    display_values()

def input_ones():
    money['Ones'] = int(input('how many one dollar coins are in your cash box? '))
    display_values()

def input_twos():
    money['twos'] = int(input('how many one two dollar coins are in your cash box? '))
    display_values()

def input_fives():
    money['fives'] = int(input('how many five dollar bills are in your cash box? '))
    display_values()

def input_tens():
    money['Tens'] = int(input('how many ten dollar bills are in your cash box? '))
    display_values()

def input_twenties():
    money['Twenties'] = int(input('how many twenty dollar bills are in your cash box? '))
    display_values()

def input_fifties():
    money['fifties'] = int(input('how many fifty dollar bills are in your cash box? '))
    display_values()

def input_one_hundreds():
    money['one hundreds'] = int(input('how many one hundred dollar bills are in your cash box '))
    display_values()

def change_value():
    entry = input("what value would you like to change?\n1. One dollar")

assign_all_values()
Jared Smith
  • 19,721
  • 5
  • 45
  • 83
Terry
  • 17
  • 6

1 Answers1

1

You're on the right track with the decimal data type. You can integrate it into your program by using decimal in replacement of integers and floats.

First, import the module
from decimal import Decimal as D

Then you can use it like so:
money['nickles'] = D(input('how many nickels are in your cash box? '))

You should define your multiplication factors and initial 0's as decimal as well. Then you can preform arithmetic (between two decimal values) just like the native python types.
Note that you should pass a string to the decimal initialization (ex. number = D('0.05')) for exact representation

When you want to print the value, you can use the .quantize(D('1.000')) to round to #0's decimal places

Nick
  • 66
  • 2
  • 1
    A note: The exact contents used for the `quantize` argument don't really matter; `x.quantize(D('0.000'))` and `x.quantize(D('9.999'))` work just fine too. All you're making the other decimal for is to get a specific exponent to copy/round to in the first decimal, and any value with a fixed length string like that works. Also note: The `decimal` docs [include a recipe for formatting as money](https://docs.python.org/3/library/decimal.html#recipes). – ShadowRanger Jan 24 '19 at 01:56
  • @ShadowRanger Yes, you are right. I also just realized I meant to use 0's instead of x's. I will update the answer - thanks for the clarification! – Nick Jan 24 '19 at 02:01
  • I think I understand Decimal now. I'm still struggling with .quantize() – Terry Jan 24 '19 at 02:08