0
1

More than 1 year has passed since last update.

指数表示

Posted at

桁数が多い数値は見づらいです。例えば2の30乗を計算してみます。

x = 2**30
print(x)

↓出力

1073741824

ぱっと見、桁数が分かりませんね。Pythonのf文字列(フォーマット済み文字列リテラル:f-string)を用い、変数名の後ろに「:e」を付けると指数表示されます。

print(f'{x:e}')

↓出力

1.073742e+09

これで桁数(この場合は10^9の桁数)が一目で分かるようになりました。eの前の数字部分(仮数部)を小数点以下何桁まで表示するかは、eの前に「.数字」を付け足します。

print(f'{x:.1e}')
print(f'{x:.2e}')
print(f'{x:.3e}')

↓出力

1.1e+09
1.07e+09
1.074e+09
0
1
0

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
1