2

I've got an issue with my money counting script. It works fine but I need to format output in two ways but I cannot use any string functions including format but I suppose to use math lib floor ceil function.

import math

coins1 = int(input("Quantity of 1 cent coins ? ")) * 0.01

coins2 = int(input("Quantity of 2 cent coins ? ")) * 0.02

coins3 = int(input("Quantity of 5 cent coins ? ")) * 0.05

coins4 = int(input("Quantity of 10 cent coins ? ")) * 0.10

coins5 = int(input("Quantity of 20 cent coins ? ")) * 0.20

coins6 = int(input("Quantity of 50 cent coins ? ")) * 0.50

coins7 = int(input("Quantity of 1 euro coins ? ")) * 1

coins8 = int(input("Quantity of 2 euro coins ? ")) * 2

bills1 = int(input("Quantity of 5 euro bills ? ")) * 5

bills2 = int(input("Quantity of 10 euro bills ? ")) * 10

bills3 = int(input("Quantity of 20 euro bills ? ")) * 20

bills4 = int(input("Quantity of 50 euro bills ? ")) * 50



total = coins1 + coins2 + coins3 + coins4 + coins5 + coins6 + coins7 + coins8 + coins1 + bills2 + bills3 + bills4


print("You've got", total, "euro and",)

My current output is:

You've got 32792039464.8 euro and

My goal is:

You've got 32792039464 euro and 80 cents.
$32.792.39.464,80
ruohola
  • 21,987
  • 6
  • 62
  • 97
Lukas
  • 31
  • 5
  • What does `math.floor(total)` give you? – Dan Jun 01 '19 at 22:52
  • Please don't edit your question to incorporate the improvements of the answers. If you feel like you need to ask something more, go ahead and ask a new question. I rolled back your question to it's original form, so that the current answers aren't outdated. – ruohola Jun 01 '19 at 23:23
  • Sorry my bad. I'm completely new here and didn't know the rules. – Lukas Jun 01 '19 at 23:32
  • 1
    Don't use float to represent money. Use an integer number of cents, and use `divmod` to get the number of euros and cents for display purposes. – chepner Jun 01 '19 at 23:36
  • FYI: [never use float for money](https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency). Rather count everything in cent instead. This will make your life easier and you can simply use the modulo operator `%` and [integer division](https://docs.python.org/3.1/tutorial/introduction.html#numbers). Also avoid names like `var1`, `var2`, `var3`, etc. Rather use `cent1 = int(input())`, `cent2 = int(input()) * 2`, `cent5 = int(intput()) * 5`, ... `euro1 = int(input()) * 100`, etc., in this case using meaningful numbers instead of just `coins` + `counter`. – Michi Jun 01 '19 at 23:37
  • I will rewrite this, thanks for the advice guys. – Lukas Jun 01 '19 at 23:55

2 Answers2

1

Just use int() to get the whole number part and modulo % to get the decimal part:

print("You've got", int(total), "euro and", total % 1, "cents.")

You will notice a classical issue of floating point numbers very soon :)

ruohola
  • 21,987
  • 6
  • 62
  • 97
1

You first need to multiply total%1 by 100 to get the value of cents in well, cents.

cents = math.ceil(total%1 * 100) ## for 32792039464.8 cents will be 0.8. times 100 is 80.
total = math.floor(total - cents/100) ## round the number down

print("You've got", total, "euro and ", cents, " cents.")

You could also use this:

cents = round(total%1 * 100) ## for 32792039464.8 cents will be 0.8. times 100 is 80 cents.
total = round(total - cents/100) ## round the total down. 32792039464.8 to 32792039464

print("You've got", total, "euro and ", cents, " cents")
  • `cents = math.ceil(total%1 * 100)` is wrong. If `total = 1.23001`, which can happen because you're counting in floats, then `cents` will be `24` instead of `23` (which would be correct in this case). Anyway, don't use float for money. – Michi Jun 01 '19 at 23:42
  • Total cannot be 1.23001 as the jumps are in one cent interval. Meaning total would go from 1.23 to 1.24. – Evyatar Reif Jun 01 '19 at 23:48
  • That's not how floats work :) See e.g. [wikipedia](https://en.wikipedia.org/wiki/Floating-point_arithmetic#Representable_numbers,_conversion_and_rounding) about floats. Also, see the nice example in the [python docu of `round()`](https://docs.python.org/3/library/functions.html#round), which also links to [floating point arithmetic issues](https://docs.python.org/3/tutorial/floatingpoint.html#tut-fp-issues). This is why you don't use floats for money. – Michi Jun 01 '19 at 23:54