Ask Your Question
0

Branching on type

asked 2025-08-22 20:33:19 +0200

JimHefferon gravatar image

I want to produce a table of numbers. If the number is an integer then I just want to show that integer. If it is a float then I want to show that float, formatted to some number of places. I want a test roughly like this.

if type(x) == int:
   outstring-= str(x)
else
    outstring = "{:.3f}".format(x)

But I can't hit on the correct type test. If you ask for type(x) then you get <class 'sage.rings.integer.Integer'> and putting that into the test doesn't work for me. How to do that? (If I am doing it all wrong, Id love to hear that also.)

edit retag flag offensive close merge delete

Comments

If you want to keep a test on type(x), it should be written as type(x) == sage.rings.integer.Integer.

eric_g gravatar imageeric_g ( 2025-08-25 15:44:55 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2025-08-23 21:19:57 +0200

Max Alekseyev gravatar image

isinstance( x, sage.rings.integer.Integer | int ) should do the job.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2025-08-22 20:33:19 +0200

Seen: 75 times

Last updated: Aug 23