Ruby | Float truncate function Last Updated : 07 Jan, 2020 Comments Improve Suggest changes Like Article Like Report 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 : Ruby # Ruby code for truncate() method # declaring float values a = 0.767 # declaring float values b = 2999.011 # declaring float values c = 2.0000 # TRUNCATED VALUES 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 : Ruby # Ruby code for truncate() method # declaring float values a = -83930.00000 # declaring float values b = -66662999.11 # TRUNCATED VALUES puts "truncate a : #{a.truncate()}\n\n" puts "truncate b : #{b.truncate()}\n\n" Output : truncate a : -83930 truncate b : -66662999 Comment More infoAdvertise with us Next Article Ruby | Float truncate function M mayank5326 Follow Improve Article Tags : Ruby Ruby-Methods Ruby Float-class Similar Reads Ruby | BigDecimal truncate() function BigDecimal#truncate() : truncate() is a BigDecimal class method which returns the Big decimal by truncating to the nearest integer (by default). Syntax: BigDecimal.truncate() Parameter: BigDecimal values Return: the Big decimal by truncating to the nearest integer (by default). Example #1 : Ruby # R 1 min read Ruby | Numeric truncate() function The truncate() is an inbuilt method in Ruby returns a number rounded toward zero with a precision of the given number of digits after the decimal point. In case the number of digits is not given, the default value is taken to be zero. Syntax: num.truncate(ndigits) Parameters: The function needs a nu 1 min read Ruby | Rational truncate() function The truncate() is an inbuilt function in Ruby returns rat truncated (toward zero) to a precision of ndigits decimal digits. ndigits by default is 0. It returns a rational when ndigits is positive, otherwise returns an integer. Syntax: rat.truncate(ndigits) Parameters: The function accepts a single p 1 min read Ruby Integer truncate() function with example The truncate() function in Ruby returns an int truncated value to a precision of ndigits decimal digits. When the precision is negative, the returned value is an integer with at least ndigits.abs trailing zeros. It returns itself when ndigits is zero or positive. The default value of ndigits is cons 1 min read Ruby | Math sqrt() function The sqrt() is an inbuilt function in Ruby returns the square root of a given value. Syntax: Math.sqrt(value) Parameters: The function accepts one mandatory parameter value whose square root is to be returned. Return Value: It returns the square root of the value. Example 1: CPP #Ruby program for sqr 1 min read Like