您的位置:首页 > 编程语言 > Ruby

[Ruby笔记]16.Ruby 判断数字 .is_a?(Numeric) .to_i

2016-05-29 08:39 288 查看

使用.is_a?(Numeric)

PS C:\Users\Administrator\RubyCode> irb --simple-prompt
>> 100.is_a?(Numeric)
=> true


使用.to_i

num.rb
,如果输入数据是数字,乘以
100
再进行
to_i
转换还是等于该数值乘以
100
的:

def num?(amount)
if (amount * 100).to_i == amount * 100
puts "The number is #{amount}"
else
puts "Number, please!"
end
end


打开
irb


PS C:\Users\Administrator\RubyCode> irb --simple-prompt
>> load 'num.rb'
=> true

>> num?(100)
The number is 100
=> nil

>> num?('hello')
Number, please!


Test if a string is basically an integer in quotes using Ruby?

https://stackoverflow.com/questions/1235863/test-if-a-string-is-basically-an-integer-in-quotes-using-ruby

reference

《The Well-Grounded Rubyist, Second Edition》

(https://www.manning.com/books/the-well-grounded-rubyist-second-edition)

3.3. Setter methods

眠いよ・・・
   /ヽ、
  /__ヾ
 ~(ηω=)ノ
 ~( ×)
   ∪∪
 http://emoji.vis.ne.jp/neruyo_3koma1.htm[/code] 
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息