Hi all,
Here i have listed some of the most frequently used math functions in ruby.
round method rounds the value
if decimal value is less than .5, rounded to its lowest value
irb(main):006:0> 1.2.round
=> 1
if decimal value is greater than .4, rounded to its highest value
irb(main):007:0> 1.5.round
=> 2
floor method rounded the value to lowest value
irb(main):008:0> 1.5.floor
=> 1
ceil method rounded the value to next highest value
irb(main):009:0> 1.5.ceil
=> 2
'nan' method used to check whether the value is integer or not
irb(main):010:0> x = 0.0/0.0
=> NaN
irb(main):011:0> x.nan?
=> true
Get ASCII value
irb(main):017:0> Integer(?e)
=> 101
irb(main):018:0> Float(?e)
=> 101.0
>> "%.2f" % (1.0/2.0)
=> "0.50"
The last method is mainly used for display purpose. We have to display the floating point number in any page(say to display the cost) we must modify the output in such a way to get clear display(Max of 2 numbers after decimal point in this case).
Thanks,
Vadivelan
No comments:
Post a Comment