Ruby Integer truncate() function with example Last Updated : 07 Jan, 2020 Comments Improve Suggest changes Like Article Like Report 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 considered to be zero. Syntax: number.truncate(ndigits) Parameter: The function takes the integer which is to be truncated and a non-mandatory ndigits, upto which it has to be truncated. Return Value: The function returns the truncated value to a precision of ndigits. Example #1: Ruby # Ruby program for truncate function # Initializing the number num1 = 120 ndigits1 = -2 num2 = 120 ndigits2 = -2 num3 = -19 ndigits3 = -1 num4 = 120 # Prints the truncated value puts num1.truncate(ndigits1) puts num2.truncate(ndigits2) puts num3.truncate(ndigits3) puts num4.truncate Output: 100 100 -10 120 Example #2: Ruby # Ruby program for truncate function # Initializing the number num1 = 190 ndigits1 = -1 num2 = 10 ndigits2 = 2 num3 = 18 ndigits3 = -1 num4 = 1123 # Prints the truncated value puts num1.truncate(ndigits1) puts num2.truncate(ndigits2) puts num3.truncate(ndigits3) puts num4.truncate Output: 190 10 10 1123 Comment More infoAdvertise with us Next Article Ruby Integer truncate() function with example G gopaldave Follow Improve Article Tags : Ruby Ruby-Methods Ruby Integer-class Similar Reads Ruby Integer to_r function with example The to_r function in Ruby converts the value of the number to a rational. Syntax: number.to_r Parameter: The function takes the number which is to be converted to a rational number. Return Value: The function returns the rational number. Example #1: Ruby # Ruby program for to_r function # Initializi 1 min read Ruby Integer size() function with example The size() function in Ruby returns the number of bytes in the machine representation of int. Syntax: number.size Parameter: The function takes the integer whose number of bytes is returned. Return Value: The function returns the number of bytes. Example #1: Ruby # Ruby program for size() function # 1 min read Ruby Integer to_s function with example The to_s function in Ruby returns a string containing the place-value representation of int with radix base (between 2 and 36). If no base is provided in the parameter then it assumes the base to be 10 and returns. Syntax: number.to_s(base) Parameter: The function takes the number and a base to whi 1 min read Ruby Integer to_f function with example The to_f function in Ruby converts the value of the number as a float. If it does not fit in float, then it returns infinity. Syntax: number.to_f Parameter: The function takes the integer which is to be converted to float. Return Value: The function returns the float value of the number. Example #1: 1 min read Ruby Integer to_int function with example The to_int function in Ruby converts the value of the number to int. If the number itself is an int, it returns self only. It is an alias of to_i function. Syntax: number.to_int Parameter: The function takes the number which is to be converted to int. Return Value: The function returns the int valu 1 min read Like