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 :
require 'bigdecimal'
require 'bigdecimal/util'
a = BigDecimal( "10" )
b = -BigDecimal( "10" )
c = -BigDecimal( "11.43" )
puts "BigDecimal a truncate method : #{a.truncate()}\n\n"
puts "BigDecimal b truncate method : #{b.truncate()}\n\n"
puts "BigDecimal c truncate method : #{c.truncate()}\n\n"
|
Output :
BigDecimal a truncate method : 10
BigDecimal b truncate method : -10
BigDecimal c truncate method : -11
Example #2 :
require 'bigdecimal'
require 'bigdecimal/util'
a = BigDecimal( '12' )* 12
b = BigDecimal( '10' )-( 22 ** 7 . 1 ) ** 10
c = BigDecimal( '-3' )
puts "BigDecimal a truncate method : #{a.truncate()}\n\n"
puts "BigDecimal b truncate method : #{b.truncate()}\n\n"
puts "BigDecimal c truncate method : #{c.truncate()}\n\n"
|
Output :
BigDecimal a truncate method : 144
BigDecimal b truncate method : -205121100730586399999999999999999999999999999999999999999999999999999999999999999999999999999990
BigDecimal c truncate method : -3
Last Updated :
06 Dec, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...