Ruby Float round() method with example
Last Updated :
08 Jan, 2020
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 :
a = 0 . 767
b = 2999 . 011
puts "rounding a : #{a.round}\n\n"
puts "rounding b : #{b.round}\n\n"
|
Output :
rounding a : 1
rounding b : 2999
Example #2 :
a = 0 . 767
b = 2999 . 011
c = 2 . 0000
puts "round a : #{a.round(2)}\n\n"
puts "round b : #{b.round(-2)}\n\n"
puts "round c : #{c.round(0)}\n\n"
|
Output :
round a : 0.77
round b : 3000
round c : 2
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...