Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Is it possible to Limit the decimals of a float?
-
@cvp you are the man don't let anybody tell you otherwise!
-
@RocketBlaster05 you are too kind. Do you agree that I forward your sentence to my wife? 😂
-
@cvp without a doubt my man
-
@cvp I've been messing around testing all the functions of the app but occasionally when I rerun the program the textview under the changetotals() function adds an extra .0000000000001 to the end of the number and I don't know why. Any Ideas?
-
I assume it is related to the delegate... If you press more than the delegate allows it bugs out the number every now and then for some reason.
-
@RocketBlaster05 Are should sure that textfield1 is a TextField not a TextView in your pyui?
-
@cvp yes
-
@RocketBlaster05
text_entered_glass is called after the delegate. Is your round still needed? -
@cvp Here is what I have it set to now:
def text_entered_paper(sender): global userinput userinput = sender.text try: input = float(userinput) if input <= 5 and input >= 0: global paptot paptot += input v.name = str(paptot) with open(fil4,mode='wt') as f: f.write(str(paptot)) m.changetotals() print (paptot) nav.close() else: ui.load_view('PaperError').present('sheet') except ValueError: ui.load_view('PaperError').present('sheet') ```
-
@RocketBlaster05 and is your userinput erroneous ?
-
@cvp it should be, as I am using the try and except for keeping track of the input. And user input is covered by the delegate
-
@RocketBlaster05 said:
input = float(userinput)
Could you add a print(userinput,input) just after to be sure
-
@cvp I'm telling it to print input yet it won't print in in the console... but I know it must be being kept track of because it goes through the if statement properly...
-
@RocketBlaster05 and print paptot before and after its increase
-
@RocketBlaster05 please use something else than the word input, it is a reserved word!
-
@cvp yeah everything is tracking properly it seems... oh nope nevermind ok yep there it is... I just printed it and for some reason after adding the input it added an extra 00000000004 to the end of it
-
@cvp ok...I've been looking at it and I added 2.11 to the previous value and it went up by 2.10999999999999... for some reason it is failing to keep track of just the 2 decimals...
-
@cvp I fixed it now... I used format in order to properly round it. In changetotals() I went to my variables and changed paptot and gltot to this format:
gltot = format(float(f.read()), '.2f') paptot = format(float(f.read()), '.2f')
-
@RocketBlaster05 Nice.
For your information, a decimal number (a number with decimals) is stored in a binary memory and is almost never exactly correct, and it is so since the invention of IT. -
@cvp ugh... one Last bug for my demo... when I do a callback on changetotals() it works every time until I open a certain ui...
class Menu(Scene): def __init__(self): self.background_color = '#4fb357' self.mm = ui.load_view('MainMenu') self.mm['imageview1'].image = ui.Image('companylogo.PNG') self.changetotals() self.mm.present('fullscreen', hide_title_bar=True) def changetotals(self): # works until learn ui is called if os.path.exists(fil): with open(fil,mode='rt') as f: global wbtot wbtot = int(f.read()) else: wbtot = 0 if os.path.exists(fil2): with open(fil2,mode='rt') as f: global cbbtot cbbtot = int(f.read()) else: cbbtot = 0 if os.path.exists(fil3): with open(fil3,mode='rt') as f: global gltot gltot = float(format(float(f.read()), '.2f')) else: gltot = 0 if os.path.exists(fil4): with open(fil4,mode='rt') as f: global paptot paptot = float(format(float(f.read()), '.2f')) else: paptot = 0 self.mm['textview1'].text = 'Water Bottles Recyled: ' + str(wbtot) self.mm['textview2'].text = 'Cardboard Boxes Recycled: ' + str(cbbtot) self.mm['textview3'].text = 'Glass Recycled: ' + str(gltot) + ' lbs' self.mm['textview4'].text = 'Paper Recycled: ' + str(paptot) + ' lbs' def button_tapped_learn(Learn): # ui button on main menu that presents bugged ui.View global m m.mm = ui.load_view('LearnPage') m.mm.present('fullscreen', hide_title_bar=True) def menu_return(Back): # process that returns to main menu... causes changetotals() to not work properly global m m.mm.close() m.mm = ui.load_view('MainMenu') m.changetotals()