1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pythonで任意の桁の値を取る

Last updated at Posted at 2025-04-05

最初に概要

この記事はプログラミング初心者がモチベーションを保つために1日に学んだことなどをひたすらに書いていくものです。

多分qiitaを見ている多くの人には全く意味のないものですが温かい目で見てくださると何よりです。

任意の桁の値を取得

任意の桁の値はインデックス[]で取得します。
整数部は一の位、十の位、百の位がそれぞれ[-1], [-2], [-3]、小数部は小数点第一位、第二位、第三位がそれぞれ[0], [1], [2]に相当しています。

つまり以下のように取得できます。

sum = 56.7231
n = str(sum)
n_a , n_b = n.split('.')

print(n_b[2])
# 3

print(n_a[-1])
# 6

print(n_b[0])
# 7

となります。
ちなみになぜn.splitで整数部と小数部で分けるのかというと小数点の{.}の部分が文字列のまま入ってしまうからだと判明しました。
納得

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?