當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Ruby BigDecimal hash用法及代碼示例

BigDecimal#hash():hash()是BigDecimal類方法,它返回BigDecimal值的哈希值。

用法: BigDecimal.hash()

參數:BigDecimal值以查找值的哈希部分


返回:BigDecimal值的哈希值

代碼#1:hash()方法示例

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

輸出:

hash value of a:-356173160844341091

hash value of b:-3967525242845538832

hash value of c:-3453672420229872218

代碼2:hash()方法示例

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

輸出:

hash value of a:-3561916586715218903

hash value of b:1852347221456838115

hash value of c:1714082193134741719


相關用法


注:本文由純淨天空篩選整理自mayank5326大神的英文原創作品 Ruby | BigDecimal class hash value。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。