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
Please Login to comment...