0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

isfloatable() floatに変換できそうかどうか判定する関数

Last updated at Posted at 2021-11-10

isfloat()が文字列に優しくなかったり
ちょっと不便な感じだったので・・。

def isfloatable(value):
    if type(value) == float :
        return True
    elif type(value) == str or type(value) == int:
        try:
            float(value)
            return True
        except ValueError:
            return False
    else:
        return False
>>> print( isfloatable(10) )
True
>>> print( isfloatable("10") )
True
>>> print( isfloatable(10.1) )
True
>>> print( isfloatable("10.1") )
True
>>> print( isfloatable("10.1a") )
False
>>> print( isfloatable("a") )
False
>>> print( isfloatable("") )
False

#参考
【Python】文字列が数値型(int・float)へ変換可能か判定する方法

0
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?