红宝石 | BigDecimal to_digits()函数
BigDecimal#to_digits() : to_digits()是一个 BigDecimal 类方法,它将 Big Decimal 转换为“nnnnnn.mmm”形式的字符串
Syntax: BigDecimal.to_digits()
Parameter: BigDecimal values
Return: converts a Big decimal to string of the form “nnnnnn.mmm”
示例 #1:
# Ruby code for BigDecimal.to_digits() method
# loading library
require 'bigdecimal'
require 'bigdecimal/util'
# declaring bigdecimal
a = BigDecimal("10")
# declaring bigdecimal
b = -BigDecimal("10")
# declaring bigdecimal
c = -BigDecimal("11.43")
# to_digits() method
puts "BigDecimal a to_digits method : #{a.to_digits()}\n\n"
puts "BigDecimal b to_digits method : #{b.to_digits()}\n\n"
puts "BigDecimal c to_digits method : #{c.to_digits()}\n\n"
输出 :
BigDecimal a to_digits method : 10.0
BigDecimal b to_digits method : -10.0
BigDecimal c to_digits method : -11.43
示例 #2:
# Ruby code for BigDecimal.to_digits() method
# loading library
require 'bigdecimal'
require 'bigdecimal/util'
# declaring bigdecimal
a = BigDecimal('12')*12
# declaring bigdecimal
b = BigDecimal('10')-(22 ** 7.1) ** 10
# declaring bigdecimal
c = BigDecimal('-3')
# to_digits() method
puts "BigDecimal a to_digits method : #{a.to_digits()}\n\n"
puts "BigDecimal b to_digits method : #{b.to_digits()}\n\n"
puts "BigDecimal c to_digits method : #{c.to_digits()}\n\n"
输出 :
BigDecimal a to_digits method : 144.0
BigDecimal b to_digits method : -205121100730586399999999999999999999999999999999999999999999999999999999999999999999999999999990.0
BigDecimal c to_digits method : -3.0