Ruby | BigDecimal round() function

BigDecimal#round() : round() is a BigDecimal class method which rounds the Big decimal to the nearest integer.

Syntax: BigDecimal.round()

Parameter: BigDecimal values

Return: rounds the Big decimal to the nearest integer.

Example #1 :



filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for BigDecimal.round() method
  
# loading library
require 'bigdecimal'
  
# declaring bigdecimal
a = 42.1**13
  
# declaring bigdecimal
b = -BigDecimal("10")
  
# declaring bigdecimal
c = -(22 ** 7.1) * 10
  
# round() method
puts "BigDecimal round example : #{a.round()}\n\n"
  
puts "BigDecimal round example : #{b.round()}\n\n"
  
puts "BigDecimal round example : #{c.round()}\n\n"

chevron_right


Output :

BigDecimal round example : 1305170490200643862528

BigDecimal round example : -10

BigDecimal round example : -33978252068

Example #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for BigDecimal.round() method
  
# loading library
require 'bigdecimal'
  
# declaring bigdecimal
a = 12**12 - 27
  
# declaring bigdecimal
b = BigDecimal('10')-(22 ** 7.1) ** 10
  
# declaring bigdecimal
c = BigDecimal('-3')
  
# round() method
puts "BigDecimal round example : #{a.round()}\n\n"
  
puts "BigDecimal round example : #{b.round()}\n\n"
  
puts "BigDecimal round example : #{c.round()}\n\n"

chevron_right


Output :


BigDecimal round example : 8916100448229

BigDecimal round example : -205121100730586399999999999999999999999999999999999999999999999999999999999999999999999999999990

BigDecimal round example : -3



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.