Ruby | Numeric truncate() function Last Updated : 19 Mar, 2024 Comments Improve Suggest changes Like Article Like Report 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 number and ndigits which specifies the number of digits to be rounded off. If ndigits is not given then, default value is taken to be zero. Return Value: It returns the rounded value. Example 1: Ruby # Ruby program for truncate() # method in Numeric # Initialize a number num1 = -16.7834 num2 = -16.78324 num3 = 16.873 # Prints truncated value puts num1.truncate(1) puts num2.truncate() puts num3.truncate() Output: -16.7 -16 16 Example 2: Ruby # Ruby program for truncate() # method in Numeric # Initialize a number num1 = 12.32 num2 = -1321.998321 num3 = -12.2321 # Prints truncated value puts num1.truncate(1) puts num2.truncate(2) puts num3.truncate(3) Output: 12.3 -1321.99 -12.232 Comment More infoAdvertise with us Next Article Ruby | Numeric truncate() function G gopaldave Follow Improve Article Tags : Ruby Ruby-Methods Ruby Numeric-class Similar Reads 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 | Numeric zero? function The zero?() is an inbuilt method in Ruby returns a boolean value. It returns true if the number is a negative one, else it returns false. Syntax: num.zero?() Parameters: The function needs a number which is to be checked for. Return Value: It returns returns a boolean value. Example 1: CPP # Ruby pr 1 min read Ruby | Numeric to_c() function The to_c() is an inbuilt method in Ruby returns a complex number with the num. Syntax: num.to_c() Parameters: The function needs the number which is to be returned as complex. Return Value: It returns the complex number. Example 1: Ruby # Ruby program for to_c() # method in Numeric # Initialize a nu 1 min read Ruby | Numeric to_int() function The to_int() is an inbuilt method in Ruby returns the integer part of the given number. Syntax: num.to_int() Parameters: The function needs the number whose integer part is to be returned. Return Value: It returns the integer part. Example 1: Ruby # Ruby program for to_int() # method in Numeric # In 1 min read Ruby | Numeric round() function The round() is an inbuilt method in Ruby returns a number rounded to a number nearest to the given number 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.round(ndigits) Parameters: T 1 min read Like