Ruby | Float truncate function
Last Updated :
07 Jan, 2020
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 :
a = 0 . 767
b = 2999 . 011
c = 2 . 0000
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 :
a = - 83930 . 00000
b = - 66662999 . 11
puts "truncate a : #{a.truncate()}\n\n"
puts "truncate b : #{b.truncate()}\n\n"
|
Output :
truncate a : -83930
truncate b : -66662999
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...