Ruby Float round() method with example
round() is a float class method which return a float value rounded to the nearest value with n digits decimal digits precision.
Syntax: float.round()
Parameter: float value as argument
Return: Float 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 round() method # declaring float value a = 0.767 # declaring float value b = 2999.011 # rounding the float value puts "rounding a : #{a.round}\n\n" # rounding the float value puts "rounding b : #{b.round}\n\n" |
Output :
rounding a : 1 rounding b : 2999
Example #2 :
# Ruby code for round() method # declaring float value a = 0.767 # declaring float value b = 2999.011 # declaring float value c = 2.0000 # rounding the float value puts "round a : #{a.round(2)}\n\n" # rounding the float value puts "round b : #{b.round(-2)}\n\n" # rounding the float value puts "round c : #{c.round(0)}\n\n" |
Output :
round a : 0.77 round b : 3000 round c : 2
Recommended Posts:
- Ruby Float nan?() method with example
- Ruby Float quo() method with example
- Ruby Float zero?() method with example
- Ruby | Float eql() method
- Ruby Float arg() method with example
- Ruby Float positive() method with example
- Ruby | Float numerator() method
- Ruby Float phase() method with example
- Ruby Float denominator() method with example
- Ruby Float to_r() method with example
- Ruby Float modulo() method with example
- Ruby Float divmod() method with example
- Ruby | Float to_i() method
- Ruby Float ceil() method with example
- Ruby Float rationalize() method with example
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.



