[RUBY] I want to truncate after the decimal point


1. Conclusion </ b>

2. How to use </ b>

3. What I learned from here </ b>

  1. Conclusion

Use the floor method </ b>!

2. How to use

def cal_points(point)
  if point < 6000
    cal_point = points * 0.05
    cal_point = points * 0.1
  puts "The point is#{cal_point.floor}Is a point"

In the above, the calculation with a decimal point in the if conditional expression is truncated from # {cal_point.floor}. The floor method is applied to the expression expansion.

Reference URL: Truncate / round up / round off numbers in Ruby

  1. What I learned from here

There are also round method (rounding) and ceil method (rounding up). You can also use ***. Round (1) to "round to the first decimal place" or ***. Round (-1) to round to the first decimal place.

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

Recommended Posts