Ruby | Rational truncate() function Last Updated : 19 Mar, 2024 Comments Improve Suggest changes Like Article Like Report 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 parameter Return Value: It returns rat truncated (toward zero) to a precision of ndigits decimal digits Example 1: CPP #Ruby program for truncate() method #Initialize rational number rat1 = Rational(-4, 3) #Prints the rational number puts rat1.truncate() Output: -1 Example 2: CPP #Ruby program for truncate() method #Initialize rational number rat1 = Rational('123.456') #Prints the rational number puts rat1.truncate(1) puts rat1.truncate(-1) Output: 617/5 120 Comment More infoAdvertise with us Next Article Ruby | Rational truncate() function gopaldave Follow Improve Article Tags : Ruby Ruby-Methods Ruby Rational-class Similar Reads Ruby | Rational to_i() function The to_i() is an inbuilt function in Ruby returns the truncated integer value Syntax: rat.to_i() Parameters: The function accepts no parameter Return Value: It returns the truncated integer value Example 1: CPP #Ruby program for to_i() method #Initialize rational number rat1 = Rational(9, -2) #Print 1 min read Ruby | Rational to_s() function The to_s() is an inbuilt function in Ruby returns value as string Syntax: rat.to_s() Parameters: The function accepts no parameter Return Value: It returns value as string Example 1: CPP #Ruby program for to_s() method #Initialize rational number rat1 = Rational(18, -4) #Prints the rational number p 1 min read Ruby | Rational to_r() function The to_r() is an inbuilt function in Ruby returns it's own value Syntax: rat.to_r() Parameters: The function accepts no parameter Return Value: It returns it's own value Example 1: CPP #Ruby program for to_r() method #Initialize rational number rat1 = Rational(18, -4) #Prints the rational number put 1 min read Ruby | Rational to_f() function The to_f() is an inbuilt function in Ruby returns the float value Syntax: rat.to_f() Parameters: The function accepts no parameter Return Value: It returns the float value Example 1: CPP #Ruby program for to_f() method #Initialize rational number rat1 = Rational(9, -2) #Prints the rational number pu 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 round() function The round() is an inbuilt function in Ruby returns rational rounded to the nearest value with a precision of ndigits decimal digits. ndigits by default is 0. It returns a rational when ndigits is positive, otherwise returns an integer. Syntax: rational.round(ndigits) Parameters: The function accepts 1 min read Ruby | Rational quo() function The quo() is an inbuilt function in Ruby returns the rational number by performing division. Syntax: rat1.quo(rat2) Parameters: The function accepts a single parameter Return Value: It returns the rational number by performing division Example 1: Ruby # Ruby program for quo() method # Initialize rat 1 min read Ruby | Rational abs() function The abs() is an inbuilt function in Ruby returns the absolute value of rational. Syntax: rational.abs() Parameters: The function accepts no parameter Return Value: It returns returns the absolute value of rational. Example 1: Ruby # Ruby program for abs() method # Initialize rational number rat1 = R 1 min read Ruby | Rational fdiv() function The fdiv() is an inbuilt function in Ruby returns float by performing division. Syntax: rat.fdiv(numeric)Parameters: The function accepts a single parameterReturn Value: It returns float by performing division  Example 1:  Ruby # Ruby program for fdiv() method # Initialize rational number rat1 = 1 min read Ruby | Rational ceil() function The ceil() is an inbuilt function in Ruby returns the smallest number greater than or equal to rat with 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.ceil(ndigits) Parameters: The function acc 1 min read Like