Python Forum
I can't figure out simple rounding to 2 decimal places!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I can't figure out simple rounding to 2 decimal places!
#1
So I missed the day of class where they went over rounding and I can't seem to get my code to always round to 2 decimal places.

I'm new to python and coding in general so go easy on me, i'm sure it's an easy answer but I just can't seem to figure it out and can't find a clear explanation.

Here's the part of the code I'm having troubles with:

while True:
try:
copies=float(input("Enter the number of copies to be printed --> "))
except ValueError:
print("Please enter a number.")
pass
else:
break


if copies < 100:
ppc=0.30
elif copies < 500 > 99:
ppc=0.28
elif copies < 750 > 499:
ppc=0.27
elif copies < 1001 > 749:
ppc=0.26
elif copies >1000:
ppc=0.25

total=(ppc*copies)


After I need to print the outputs of the price per copy (ppc) and the total price (ppc*copies). Easy enough until I try to make sure the values come out formatted as currecy, so 2 decimals (1.25, 1.30, etc.)

Can someone give me a hand and either write the last line of code I can evaluate or explain what I may need to do? I've looked at other examples for things like %0.2f % or whatever it is but I don't understand the lingo well enough to fit this to my assignment.

Thanks in advance.
Reply
#2
Please put your code in Python code tags, and full error traceback message in error tags. You can find help here.
Reply
#3
Thank you for looking but I believe I've finally figured it out. Will delete this thread if I can otherwise (since i'm new around these parts) if a mod handles that please do. Thanks!
Reply
#4
Ok I'm glad to hear you got your solution. Thread deleting isn't necessary, don't worry. But you are welcome to post your solution in case you'd like to share.
Reply
#5
Ok. So for the random new guy like me, here's what I figured out. I was having issues rounding float numbers to simple double digit decimals. So instead of seeing numbers like 139.89999999 or something I wanted to cut off the everything but the .89. *please note this is not rounding*

while True:
    try:
        copies=float(input("Enter the number of copies to be printed --> "))
    except ValueError:
        print("Please enter a number.")
        pass
    else:
        break
    
    
if copies < 100:
    ppc=0.30
elif copies < 500 > 99:
    ppc=0.28
elif copies < 750 > 499:
    ppc=0.27
elif copies < 1001 > 749:
    ppc=0.26
elif copies >1000:
    ppc=0.25

total=(ppc*copies)
When given certain numbers the ppc (price per copy) and total cost values were coming out weird sometimes or would end in $1.3 instead of $1.30. I wont claim to know all the details but adding {0:,.2f}".format(ppc) instead of just printing the ppc or total cuts off everything but 2 decimals. So the rest of the code is :


print("The price per copy is ${0:,.2f}".format(ppc), ".")
print("The total price is ${0:,.2f}".format(total), ".")
I know the comma after the colon adds a comma to your number every 3 digits and you can change the 2 to something else if you wanted to show more/less after the period but to be honest I don't know what the 0 before the comma and the f after 2 mean. I'll figure it out as soon as my other assignments are done and see if I can update/explain here if anyone needs/wants me to. Though I'm sure there are posts on it somewhere around here.

Thanks again.
Reply
#6
To find out about what "f" does, you can refer this docs page.
0 is the index of item in the tuple you pass to .format(). In your case you pass it only one value (either ppc or total), so 0 just means first item, and it can even be ommitted in this case. But you could have something like .format(first_value, second_value, third_value), and in such case {2:} would mean third_value.

By the way, I believe your if/elif conditions will not work the way you want them to. For example:
elif copies < 500 > 99:
first copies < 500 is evaluated. If it is true, then 500 > 99 is evaluated, which is always true.
You probably want:
elif 99 < copies < 500:
Reply
#7
I think it works my way, I tested a bunch of numbers and the output matches the criteria for the assignment. My brains done for the day anyway so I'll take what I get for the grade (I'm sure it's fine, he's a lazy professor.)

But I see what you're saying, I knew there was an easier/cleaner way of doing it but for whatever reason I couldn't see it at the time. I'll make sure to keep it in mind next time.

Thanks for explaining the 0. Believe it or not I actually had that stored somewhere in my mind and as soon as I read your explanation it clicked. I'll check out the link about f.
Reply
#8
I see now, it does work indeed. However, it works mostly because (first) numbers in < conditions being checked against are in ascending order.
If the order of checking was switched in any way, it would not work anymore. In some cases it could lead to a nasty hard-to-find bug, so it is not a good habit to develop.
Also, in these cases:
elif copies < 500 > 99:
    ppc=0.28
elif copies < 750 > 499:
    ppc=0.27
elif copies < 1001 > 749:
for reason stated in last post, the second check (>) has no effect, it is always true.

I hope you get a good score on the assignment!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm trying to figure out if this is a bug from a simple quiz at Future Skill fabstr1 3 1,573 Jan-25-2023, 10:06 PM
Last Post: deanhystad
  Binary to decimal, print the decimal rs74 3 2,113 Jul-12-2020, 05:25 PM
Last Post: DPaul
  Rounding without using Python embedded functions NewCoder 3 2,759 Sep-06-2018, 10:23 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020