LoginSignup
0

More than 5 years have passed since last update.

Integer#truncateを使って1の位を切り捨てる

Posted at

環境

ruby 2.5.1

たとえば人の年齢で、
29歳 -> 20代
14歳 -> 10代
みたいに1の位を切り捨てたい場合はこうする :point_down:

29.truncate(-1)
#=> 20
14.truncate(-1)
#=> 10

ちなみに今回の要件だと、floorでも良い。(負数の挙動が異なる)
切り上げる場合はceil、四捨五入の場合はroundを使う。

以上 :hugging:

参考

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