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


Ruby BigDecimal PI()用法及代碼示例


BigDecimal#PI():PI()是BigDecimal類方法,它將pi的值返回到指定的精度位數(即數字)。

用法:BigDecimal.PI()

參數:BigDecimal值


返回:pi的值等於指定精度的數字位數。

示例1:

# Ruby code for BigDecimal.PI() method 
  
# loading library 
require 'bigdecimal'
require 'bigdecimal/util'
  
require "bigdecimal/math"
  
include BigMath 
  
# declaring bigdecimal 
a = BigMath.PI(10).to_s 
  
# declaring bigdecimal 
b = BigMath.PI(102).to_s 
  
# declaring bigdecimal 
c = BigMath.PI(2).to_s 
  
# PI() method 
puts "BigDecimal a PI method : #{a}\n\n"
  
puts "BigDecimal b PI method : #{b}\n\n"
  
puts "BigDecimal c PI method : #{c}\n\n"

輸出:

BigDecimal a PI method : 0.3141592653589793238462643388813853786957412E1

BigDecimal b PI method : 0.3141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306913173228884424E1

BigDecimal c PI method : 0.31415926535897932388671233672993238433E1

示例2:

# Ruby code for BigDecimal.PI() method 
  
# loading library 
require 'bigdecimal'
require 'bigdecimal/util'
  
require "bigdecimal/math"
  
include BigMath 
  
# declaring bigdecimal 
a = BigMath.PI(78).to_s 
  
# declaring bigdecimal 
b = BigMath.PI(12).to_s 
  
# declaring bigdecimal 
c = BigMath.PI(1).to_s 
  
# PI() method 
puts "BigDecimal a PI method : #{a}\n\n"
  
puts "BigDecimal b PI method : #{b}\n\n"
  
puts "BigDecimal c PI method : #{c}\n\n"

輸出:

BigDecimal a PI method : 0.31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253422571844827786301E1

BigDecimal b PI method : 0.314159265358979323846264338336544487694763327962E1

BigDecimal c PI method : 0.31415926535897932364198143965603E1


相關用法


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