どうも、ちょげ(@chogetarou)です。
mathを使って、数値の小数点第2位を切り捨てる方法を紹介します。
方法

mathを使って、数値の小数点第二位を切り捨てるには、mathのfloor()を使います。
まず、mathをインポートします。
import math
次に、mathからfloor()を呼び出します。
math.floor()の引数に10を掛けた数値を指定します。
そして、floor()の結果を10で割ります。
result = math.floor(value * 10) / 10
上記のfloor()を10で割った値は、floor()の引数に指定した数値の小数点第二位を切り捨てた数値になります。
使用例
import math
result1 = math.floor(1.45 * 10) / 10
result2 = math.floor(10.27 * 10) / 10
result3 = math.floor(3.39999 * 10) / 10
print(result1) #1.4
print(result2) #10.2
print(result3) #3.3
コメント