Python Forum
test if variable is Numeric?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
test if variable is Numeric?
#11
(May-25-2018, 07:05 AM)scidam Wrote: In any case, answers should be not only correct, but also clever.

I disagree. If the code is "clever", then it's probably hard to understand.
Reply
#12
A general function to determine type 'int', 'float' or 'string' might look something like:
def get_int_or_float(v):
    # NOTE: '1.0' will be identified as type 'int'
    try:
        number_as_float = float(v)
        number_as_int = int(number_as_float)
        if number_as_float == number_as_int:
            return number_as_int
        else :
            return number_as_float
    except:
        return v

num1 = '1'
num1 = get_int_or_float(num1)
print("Input value '{}' is of type {}.".format(num1, type(num1)))

num1 = '1a'
num1 = get_int_or_float(num1)
print("Input value '{}' is of type {}.".format(num1, type(num1)))

num1 = '1.1'
num1 = get_int_or_float(num1)
print("Input value '{}' is of type {}.".format(num1, type(num1)))

num1 = 1
num1 = get_int_or_float(num1)
print("Input value '{}' is of type {}.".format(num1, type(num1)))

num1 = 1.1
num1 = get_int_or_float(num1)
print("Input value '{}' is of type {}.".format(num1, type(num1)))
Output:
Input value '1' is of type <class 'int'>. Input value '1a' is of type <class 'str'>. Input value '1.1' is of type <class 'float'>. Input value '1' is of type <class 'int'>. Input value '1.1' is of type <class 'float'>.
Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply
#13
This is my take - form a production code (I use it to load ini files - thus the name)

def convert_config_value(value):
    for numeric_type in (int, float):
        try:
            converted_value = numeric_type(value)
            return converted_value
        except ValueError:
            pass
    return value

Ooops, seems like I misread the question (I saw the post by @ljmetzger - and misinterpreted the OP). Of course, this a method for string conversion.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Numeric Enigma Machine idev 8 248 Yesterday, 02:17 PM
Last Post: idev
Question Numeric Anagrams - Count Occurances monty024 2 1,475 Nov-13-2021, 05:05 PM
Last Post: monty024
  How to get datetime from numeric format field klllmmm 3 1,962 Nov-06-2021, 03:26 PM
Last Post: snippsat
  How to test and import a model form computer to test accuracy using Sklearn library Anldra12 6 3,068 Jul-03-2021, 10:07 AM
Last Post: Anldra12
  Extract continuous numeric characters from a string in Python Robotguy 2 2,583 Jan-16-2021, 12:44 AM
Last Post: snippsat
  How to calculate column mean and row skip non numeric and na Mekala 5 4,835 May-06-2020, 10:52 AM
Last Post: anbu23
  Alpha numeric element list search rhubarbpieguy 1 1,745 Apr-01-2020, 12:41 PM
Last Post: pyzyx3qwerty
  How to write test cases for a init function by Unit test in python? binhduonggttn 2 3,066 Feb-24-2020, 12:06 PM
Last Post: Larz60+
  How to write test cases by Unit test for database configuration file? binhduonggttn 0 2,512 Feb-18-2020, 08:03 AM
Last Post: binhduonggttn
  convert a character to numeric and back Skaperen 2 2,072 Jan-28-2020, 09:32 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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