Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #49279 > unrolled thread

Need help removing trailing zeros

Started bybandcamp57@gmail.com
First post2013-06-26 15:02 -0700
Last post2013-06-26 19:30 -0700
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Need help removing trailing zeros bandcamp57@gmail.com - 2013-06-26 15:02 -0700
    Re: Need help removing trailing zeros Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-26 23:14 +0100
    Re: Need help removing trailing zeros PyNoob <bandcamp57@gmail.com> - 2013-06-26 15:21 -0700
      Re: Need help removing trailing zeros Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-27 00:05 +0100
    Re: Need help removing trailing zeros PyNoob <bandcamp57@gmail.com> - 2013-06-26 19:30 -0700

#49279 — Need help removing trailing zeros

Frombandcamp57@gmail.com
Date2013-06-26 15:02 -0700
SubjectNeed help removing trailing zeros
Message-ID<55bb3a29-7c8a-402c-8fb5-d12b5ee6330b@googlegroups.com>
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():
    def Add():
        A = float(input("Enter the first number you want to add: "))
        B = float(input("Enter the second number you want to add: "))
        Answer = A+B
        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: "))
        Answer = A-B
        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: "))
        Answer = A*B
        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: "))
        Answer = A/B
        print("The quotient of", A, "and", B, "is: ", Answer)
        print("")
        Main()

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

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

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

Main()

[toc] | [next] | [standalone]


#49281

FromJoshua Landau <joshua.landau.ws@gmail.com>
Date2013-06-26 23:14 +0100
Message-ID<mailman.3905.1372284893.3114.python-list@python.org>
In reply to#49279
On 26 June 2013 23:02,  <bandcamp57@gmail.com> wrote:
> 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")

[toc] | [prev] | [next] | [standalone]


#49282

FromPyNoob <bandcamp57@gmail.com>
Date2013-06-26 15:21 -0700
Message-ID<6cd21bc6-8437-4aec-99b0-59a1d9c7bd9b@googlegroups.com>
In reply to#49279
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))?

[toc] | [prev] | [next] | [standalone]


#49283

FromJoshua Landau <joshua.landau.ws@gmail.com>
Date2013-06-27 00:05 +0100
Message-ID<mailman.3906.1372287981.3114.python-list@python.org>
In reply to#49282
On 26 June 2013 23:21, PyNoob <bandcamp57@gmail.com> wrote:
> 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.

So instead of:
    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.

[toc] | [prev] | [next] | [standalone]


#49292

FromPyNoob <bandcamp57@gmail.com>
Date2013-06-26 19:30 -0700
Message-ID<92cc6766-92dd-41e8-bdb4-e2b892cbc71a@googlegroups.com>
In reply to#49279
I get it now! Thank you so much for your help, I really appreciate it. :)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web