Python Forum
remove zeros at the end of a number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
remove zeros at the end of a number
#1
Hello,

I am trying to remove the .0000 in the result of my window for variable carres and cubes.

I tried truncate, int, round but nothing helps.

from tkinter import *
import math

def afficher():

    nbs = range(20,41)

    for n in nbs:
     carres = n**2
     cubes = n**3

     resultat.insert(INSERT, str(n) + '\t' + '%0.4f' % round(carres)+ '\t' + '%0.4f' % cubes + '\n')


fen1 = Tk()
fen1.title('Afficher les cubes et carrés')

resultat = Text(fen1)
resultat.config(width=40, height= 50)
resultat.insert(INSERT, 'Nombres\tCarrés\tCubes\n============================\n')
resultat.grid(row =1, column = 0, columnspan = 2)

bouton1 = Button(fen1, text = "Afficher", command = afficher)
bouton1.grid(row = 1, column = 0)

bouton2 = Button(fen1, text = "Quitter", command = fen1.destroy)
bouton2.grid(row = 1, column = 1)

fen1.mainloop()
Output:
20 400.0000 8000.0000 21 441.0000 9261.0000 22 484.0000 10648.0000 23 529.0000 12167.0000 24 576.0000 13824.0000 25 625.0000 15625.0000 26 676.0000 17576.0000 27 729.0000 19683.0000 28 784.0000 21952.0000 29 841.0000 24389.0000
Any pointers?

Thank you
Reply
#2
That's funny. You are adding the four decimal places when you set the format to '%0.4f'. They wouldn't be there normally since carres and cubes are both ints. If you want to pad the value out to 4 spaces use "%4d".

But you should not use this horrible way of building formatted strings. Use f' strings instead

If you just want to format the results for printing.
import math
print(f"{math.pi}, 4 decimal places {math.pi:.4f}  No decimal places and padded to 5 spaces {math.pi:5.0f}")
Output:
3.141592653589793, 4 decimal places 3.1416 No decimal places and padded to 5 spaces 3
Frankduc likes this post
Reply
#3
Do you need to put the f in front of each variable?

resultat.insert(INSERT, f'{str(n):.0f}' + '\t' + f'{carres:.0f}' + '\t' + f'{cubes:.0f}' + '\n'')
Reply
#4
Change this line to.
resultat.insert(INSERT, str(n) + '\t' + '%d' % round(carres)+ '\t' + '%0.4f' % cubes + '\n')
# To
resultat.insert(tk.INSERT, f"{n}\t{carres}\t{cubes}\n")

So don't use old string formatting(%) learn f-string,also as shown over.
Also don't use * in import is never ok,even if many GUI tutorials do this.
Try to keep 4-space indentation in all code,so to make these changes it will be like this.
import tkinter as tk
import math

def afficher():
    nbs = range(20, 41)
    for n in nbs:
        carres = n**2
        cubes = n**3
        resultat.insert(tk.INSERT, f"{n}\t{carres}\t{cubes}\n")

fen1 = tk.Tk()
fen1.title("Afficher les cubes et carrés")
resultat = tk.Text(fen1)
resultat.config(width=40, height=50)
resultat.insert(tk.INSERT,"Nombres\tCarrés\tCubes\n============================\n",)
resultat.grid(row=1, column=0, columnspan=2)
bouton1 = tk.Button(fen1, text="Afficher", command=afficher)
bouton1.grid(row=1, column=0)
bouton2 = tk.Button(fen1, text="Quitter", command=fen1.destroy)
bouton2.grid(row=1, column=1)
fen1.geometry('300x450')
fen1.mainloop()
BashBedlam and Frankduc like this post
Reply
#5
You do not want to do this:
resultat.insert(INSERT, f'{str(n):.0f}' + '\t' + f'{carres:.0f}' + '\t' + f'{cubes:.0f}' + '\n'')
First off carres and cubes don't have any decimal places, so using ".0f" is not needed to hide the decimal part.
resultat.insert(INSERT, f'{n}' + '\t' + f'{carres}' + '\t' + f'{cubes}' + '\n')
Second, let the f'string do all the formatting. Don't make little pieces and concatenate.
resultat.insert(INSERT, f"{n}\t{carres}\t{cubes}\n")
To prevent ragged columns you probably want to pad the numbers to the same width.
resultat.insert(INSERT, f"{n:2}\t{carres:4}\t{cubes:4}\n")
Frankduc likes this post
Reply
#6
Wow amazing!
That is not in the book. I was following the books teacher.
I didn't put that *, pycharm impose it to me. I just started using it. But i dont like it at all. Its heavy and not user friendly.

Thank you snipp
Reply
#7
pycharm does not "impose" wildcard imports. It might suggest such a thing, but you can type whatever you please.

Your book is probably too old. f'string formatting was introduced in Python 3.6. You should not rely on any book as the single source of information, and you should not be relying on this forum as your #2 source. After reading about something in you book, google it and see what others have to say about the topic. Look for examples of how a new language feature is used. Maybe do a tutorial.
Reply
#8
Book is dated 2020. I check on stackoverflow and other sites but info are sometimes contradictory. I use the forum as another source of info. But i am not disappointed, people are nice here, give good and fast info. Maybe not perfect but most of the time reliable. I wouldn't say impose but everytime i use import math or any other module it put automatically a * next to it. I can delete it, true, but its coming back.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Remove Specific Columns when the number of columns is greater than a specific value CuriousOne 0 1,325 Sep-09-2021, 09:17 PM
Last Post: CuriousOne
  Solving for zeros of an equation! fmitchell17 0 1,837 Apr-05-2021, 07:49 PM
Last Post: fmitchell17
  Dataframe Removes Zeros JoeDainton123 2 4,402 Sep-17-2020, 12:47 PM
Last Post: scidam
  How to keep leading zeros with pandas? eeps24 1 6,576 May-20-2020, 07:51 PM
Last Post: deanhystad
  Probs with leading zeros falling away :-) Badosc 2 2,884 Dec-04-2018, 08:57 PM
Last Post: Badosc
  Controlling trailing zeros with rounding? RedSkeleton007 1 24,510 Jan-25-2018, 07:23 AM
Last Post: j.crater
  leading zeros kerzol81 7 7,096 Apr-23-2017, 03:53 PM
Last Post: kerzol81
  program is related to leading zeros amrita 3 3,839 Apr-13-2017, 06:57 AM
Last Post: wavic

Forum Jump:

User Panel Messages

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