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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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"

chevron_right


Output :

rounding a : 1

rounding b : 2999

Example #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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"

chevron_right


Output :

round a : 0.77

round b : 3000

round c : 2


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.