LoginSignup
0
0

More than 5 years have passed since last update.

Python > int('1e3') > ValueError: invalid literal for int() with base 10: '1e3' / str(1e3) > '1000.0'

Last updated at Posted at 2017-01-19
動作環境
Xeon E5-2620 v4 (8コア) x 2
32GB RAM
CentOS 6.8 (64bit)
openmpi-1.8.x86_64 とその-devel
mpich.x86_64 3.1-5.el6とその-devel
gcc version 4.4.7 (とgfortran)
NCAR Command Language Version 6.3.0
WRF v3.7.1を使用。

@ Introducing Python: Modern Computing in Simple Packages by Bill Lubanovic

int to string

int()を用いる場合、int('1e3')のような指数形式の記載でエラーが出ることが上記の本に書かれていた (@ No.733 / 12833)。

試してみた。

int_func_170120.py
ival1=int('1000')
print(ival1)
ival2=int('1e3')
print(ival2)
結果
$ python int_func_170120.py 
1000
Traceback (most recent call last):
  File "int_func_170120.py", line 3, in <module>
    ival2=int('1e3')
ValueError: invalid literal for int() with base 10: '1e3'

string to int

上記をしていて気になったのは、1e3をstr変換した場合はどうか、ということ。

試してみた。

str_func_170120.py
str1=str(1000)
print(str1)
str2=str(1e3)
print(str2)
結果
1000
1000.0

1e3という数値は1000.0に変換された上でstring型になっているようだ。

上記の本の (@ No. 831 / 12833)にも記載があった。

0
0
1

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