---
title: "rubyonrails:数字"
author: "PBFox"
date: "2007-11-15T16:45:00Z"
tags: []
slug: "rubyonrails"
draft: false
lastmod: "2007-11-15T16:45:00Z"
id: 479255
url: "https://developerwork.iteye.com/blog/479255"
---

‘400’.to\_i #=&gt;400  
'405' .to\_i(8) #=&gt;261  
'405'.to\_i(16) #=&gt;1029

随机数：  
rand(5) #=&gt;2  
rand(5) #=&gt;1  
rand(5) #=&gt;5  
rand(1000) #=&gt;369

获取对数：  
Math.log(1) #=&gt;0.0  
Math.log(Math::E) #=&gt;1.0  
Math.log(10) #=&gt;2.302585...

平均数：  
def mean (array)  
array.inject(0) {|sum,x| sum+=x} /array.size.to\_f  
end  
mean(\[1,2,3,4]) #=&gt;2.5  
mean(\[100,100,100,100,1]) #=&gt;100.025