# Need help removing trailing zeros

B

#### bandcamp57

Hello, i'm making a calculator and I want to be able to use decimals but I don't like it when it comes out as ex.12.0 when it should be 12. I tried using .rstrip("0".rstrip(".") but that never seemed to work. If anyone has a solution please let me know, all help is greatly appreciated.

Code:

def Main():
print(" Welcome to the Calculator! What would you like to do?")
print("")
print("1) Basic Operations")

user_input = input("Enter the number corresponding with your choice: ")

if user_input == "1":
BasicOperations()

def BasicOperations():
A = float(input("Enter the first number you want to add: "))
B = float(input("Enter the second number you want to add: "))
print("The sum of", A, "and", B, "is: ", Answer)
print("")
Main()

def Sub():
A = float(input("Enter the first number you want to subtract: "))
B = float(input("Enter the second number you want to subtract: "))
print("The difference between", A, "and", B, "is: ", Answer)
print("")
Main()

def Mul():
A = float(input("Enter the first number you want to multiply: "))
B = float(input("Enter the second number you want to multiply: "))
print("The product of", A, "and", B, "is: ", Answer)
print("")
Main()

def Div():
A = float(input("Enter the first number you want to divide: "))
B = float(input("Enter the second number you want to divide: "))
print("The quotient of", A, "and", B, "is: ", Answer)
print("")
Main()

print("You have selected Basic Operations.")
print("2) Subtraction")
print("3) Multiplication")
print("4) Division")
print("")

choice = input("Select the number corresponding with your choice: ")

if choice == "1":
elif choice == "2":
Sub()
elif choice == "3":
Mul()
elif choice == "4":
Div()

Main()

J

#### Joshua Landau

Hello, i'm making a calculator and I want to be able to use decimals but I don't like it when it comes out as ex.12.0 when it should be 12. I tried using .rstrip("0".rstrip(".") but that never seemed to work. If anyone has a solution please let me know, all help is greatly appreciated.

Code:
<LOTS 'O CODE>

Was that really necessary?

All you needed to give use was "print(1.0)"; why post so much?

Either:
"{:g}".format(1.0)

or, if you hate scientific notation:
"{:f}".format(1.0).rstrip(".0")

P

#### PyNoob

Sorry about that... And thanks for your help, but I don't quite understand. Would that make it off your example print("{:g}".format(1.0))?

J

#### Joshua Landau

Sorry about that... And thanks for your help, but I don't quite understand.
That's fine, but...
Would that make it off your example print("{:g}".format(1.0))?
I don't understand this sentence.

But, hey, I forgot to check what level you were working at -- there's
little point running ahead of what you know.

Did you try:
print("{:g}".format(1.0))
? It works for me. So, yes, that's what you want.

print("The quotient of", A, "and", B, "is: ", Answer)

you want
print("The quotient of", A, "and", B, "is: ", "{:g}".format(Answer))

See how I just used "{:g}".format as some kind of magic-fixing-power?

Well, why does it work?

[See http://docs.python.org/3/library/stdtypes.html#str.format and the
sub-links for a more full explanation]

Run each of these in an interpreter:

"{} {} {} {} {}".format("This", "is", "a", "formatted", "string!")

"It is really useful: I have {} cows and {}
sheep".format(number_of_cows, number_of_sheep)

"It gives me back a formatted {}, which I can
print".format(type("".format()).__name__)

"I can also give things indexes: {3} {2} {1} {0}".format("Print",
"in", "reverse", "order")

"I can also give things names: {egg} {ham}
{flies}".format(egg="Are", ham="you", flies="there?")

"It's not just {:!<10}".format("that")

"It lets me choose how I want things to be printed: {0 ^6},
{0:~>10}, {0:Â£<8}".format("Hi")

"And for numbers: {0:e}, {0:.10%}, {0:#.1f}".format(123.456)

So you just want the best formatter; see
[http://docs.python.org/3/library/string.html#format-specification-mini-language]

Your best choice is "{:g}" which just means "general format".

Note that you can also write your prints as so:

print("The quotient of {} and {} is: {:g}".format(A, B, Answer))

Whether you prefer it is your choice.

------

In regards to .rstrip: It will only work on strings; so you need to
convert like so:
str(1.0).rstrip("0").rstrip(".")

And note that:
1) My .rstrip("0.") was wrong and foolish (try str(10).rstrip("0."))
2) This needs the string to be reliably formatted in this style:
"{:#f}" *and* requires it to be a float.

So really, you'd need:

"{:#f}".format(float(number)).rstrip("0").rstrip(".")

Which is ugly, but I guess it works.

I get it now! Thank you so much for your help, I really appreciate it. 