Float#truncate() is a float class method which return a truncated value rounded to ndigits decimal digits precision.
Syntax: float.truncate()
Parameter: float value as argument
Return: truncated value rounded to nearest precision
If precision is -ve : integer with at least ndigits.abs trailing zeros
If ndigits is +ve : a floating point number, otherwise integer
Example #1 :
# Ruby code for truncate() method # declaring float values a = 0.767 # declaring float values b = 2999.011 # declaring float values c = 2.0000 # TRUNCATED VALUES puts "truncate a : #{a.truncate()}\n\n" puts "truncate b : #{b.truncate()}\n\n" puts "truncate c : #{c.truncate()}\n\n" |
Output :
truncate a : 0 truncate b : 2999 truncate c : 2
Example #2 :
# Ruby code for truncate() method # declaring float values a = -83930.00000 # declaring float values b = -66662999.11 # TRUNCATED VALUES puts "truncate a : #{a.truncate()}\n\n" puts "truncate b : #{b.truncate()}\n\n" |
Output :
truncate a : -83930 truncate b : -66662999
Recommended Posts:
- Ruby Integer truncate() function with example
- Ruby | Rational truncate() function
- Ruby | Numeric truncate() function
- Ruby | BigDecimal truncate() function
- Ruby Float to_r() method with example
- Ruby Float coerce() method with example
- Ruby | Float Class
- Ruby Float to_d() - BigDecimal method with example
- Ruby Float zero?() method with example
- Ruby Float rationalize() method with example
- Ruby Float quo() method with example
- Ruby Float prev_float() method with example
- Ruby Float next_float() method with example
- Ruby Float nan?() method with example
- Ruby Float modulo() method with example
- Ruby Float magnitude() method with example
- Ruby Float fdiv() method with example
- Ruby Float negative() method with example
- Ruby Float divmod() method with example
- Ruby | Float numerator() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

