##### converting user input to float troubles
 converting user input to float troubles RecklessTechGuy Unladen Swallow Posts: 1 Threads: 1 Joined: Aug 2020 Reputation: 0 Aug-17-2020, 10:40 AM The comments in my script says it all, but to summarize, sometimes when this code adds say "5.24 or 3.46" or any other non integer number to the calculation it causes the result to be something like ex. 56.789999999996 when it "should" be only 2 decimals like 5.00. ```#this script is an attempt to create a simple shopping calculator #I am brand new to programming and am well aware there may be #much better ways to do this. #I am open to suggestions to different methods of reaching this goal #but also an explanation to why this is happening and possibly how to #fix it in this context would be much appreciated. #gets user input and adds it to total. while True: #gets user input user_input=input("") #stops an error where no input causes the next if statement to try to add a string to an integer if not user_input: user_input=0 #converts user input from a string to a float and adds it to the total. #for some reason unknown to me, this function sometimes causes the total to become #something like ex. 54.999999999996 after adding some non int inputs like ex. 5.23? #it probably has something to do with how python converts a string to a float? maybe? if user_input: total=total + float(user_input) print(total,"+") ``` Reply ndc85430 Da Bishop Posts: 1,147 Threads: 0 Joined: Apr 2017 Reputation: 76 Aug-17-2020, 10:48 AM Calculations with floating point values aren't exact. See, for example this. Reply GOTO10 Lumberjack Posts: 130 Threads: 3 Joined: Apr 2020 Reputation: 22 Aug-17-2020, 11:10 AM You can use the round() function to round floats to two decimal points, but you still may face some issues due to the quirks of floating point math. You might want to consider using the decimal module for greater precision when working with currency. Reply deanhystad Da Bishop Posts: 1,254 Threads: 5 Joined: Feb 2020 Reputation: 132 Aug-17-2020, 12:41 PM (This post was last modified: Aug-17-2020, 12:41 PM by deanhystad.) As mentioned by ndc85430 a fixed number of binary bits cannot represent every number, even numbers with no fractional values. For your purposes you don't have to concern yourself with the tiny difference between the binary and decimal value unless your shopping calculation is going to handle millions of purchases. To get a pretty number with two decimal places use formatting in your print statement. Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Loop back through loop based on user input, keeping previous changes loop made? hbkpancakes 2 136 Nov-21-2020, 02:35 AM Last Post: hbkpancakes user input for multi-dimentional list without a prior iteration using input() Parshaw 6 353 Sep-22-2020, 04:46 PM Last Post: Parshaw How to time a user's input Tbot1000 2 279 Aug-31-2020, 09:39 PM Last Post: Larz60+ csv troubles DPaul 4 310 Aug-13-2020, 05:18 PM Last Post: DPaul Validating user input WJSwan 2 360 Jul-06-2020, 07:21 AM Last Post: menator01 Hi, I need help with defining user's input and applying it to code. jlmorenoc 2 454 Jun-24-2020, 02:10 PM Last Post: pyzyx3qwerty if the input is not number, let user input again teatea 14 969 Jun-02-2020, 07:03 PM Last Post: ibutun How to allow user input only if certain requirements are met vininhosts 3 460 May-28-2020, 06:15 PM Last Post: micseydel Installation troubles on Win 10 peringek 0 260 May-13-2020, 09:34 PM Last Post: peringek Python - Most effective way to correct keyboard-user-input. ppel123 8 695 Apr-08-2020, 07:41 AM Last Post: ppel123

Forum Jump:

### User Panel Messages

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