Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
is it float or is it int?
#1
i have a big number in a string (maybe type unicode in Python2) to convert to either float or int. i want it to be float if float can represent it and if float isn't the best (if int or long can represent it exactly) then i want it to be int (probably long in Python2). it may be in a form supported by int() with a 2nd argument of 0 such as '0x3' (won't work in float(). it may be in an exponential form like '3e0' (won't work in int()). my thinking is that at least one of these is done under try/except. any suggestions on how to do this?

i am wanting to do this in a function that is given the string (maybe type unicode in Python2) which returns the value or None if neither float nor int/long can represent it.

code simplicity is preferred. not importing a module is preferred but if a module is imported it must be integral to Python and available back as far as Python 2.6 and Python 3.0.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
What have you tried?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
i have tried nothing, yet. i'm still trying to figure it out. what if you convert a string two different ways and get two numbers that are different? which is correct?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python calculate float plus float is incorrect? sirocawa 6 207 Apr-16-2024, 01:45 PM
Last Post: DeaD_EyE
  Comaparing Float Values of Dictionary Against A Float Value & Pick Matching Key firebird 2 3,376 Jul-25-2019, 11:32 PM
Last Post: scidam

Forum Jump:

User Panel Messages

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