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 3 years have passed since last update.

小数点以下を切り捨てたい

Posted at

【概要】

1.結論

2.どのように使うのか

3.ここから学んだこと

1.結論

floorメソッドを使う!


2.どのように使うのか ----------------------------------------
def cal_points(point)
  if point < 6000
    cal_point = points * 0.05
  else
    cal_point = points * 0.1
  end
  puts "ポイントは#{cal_point.floor}点です"
end

上記ではif条件式の中で小数点が入った計算を#{cal_point.floor}を小数点以下を切り捨てています。
式展開にfloorメソッドを適用しています。

参考URL:
Rubyで数値の切り捨て・切り上げ・四捨五入する

3.ここから学んだこと

他にもroundメソッド(四捨五入),ceilメソッド(切り上げ)があります。また、.round(1)とすることで"小数点第1位まで四捨五入"や.round(-1)とすることで1の位を四捨五入できます。

ex)1.23.round(1) #1.2
ex)123.round(-1) #120

0
0
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
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?