0
Number ConversionsRuby Method of the MonthKevin Munc - @muncman
YOU JUST NEED AN INTEGER?RUBY CAN DO THAT.
Float to Integer
Float to IntegerInteger(123.45)
Float to IntegerInteger(123.45)              => 123
Float to Integer
Float to IntegerInteger(999.99)
Float to IntegerInteger(999.99)              => 999
Float to IntegerInteger(999.99)           No                       rounding              => 999
Octal to Integer
Octal to IntegerInteger(01234)
Octal to IntegerInteger(01234)                 => 668
Hexadecimal to Integer
Hexadecimal to IntegerInteger(0x1234)
Hexadecimal to IntegerInteger(0x1234)              => 291
Binary to Integer
Binary to IntegerInteger(0b101)
Binary to IntegerInteger(0b101)   ‘b’ for   binary
Binary to IntegerInteger(0b101)   ‘b’ for   binary        => 5
Strings to Integers
Strings to IntegersInteger(“1234”)
Strings to IntegersInteger(“1234”)              => 1234
Strings to Integers
Strings to IntegersInteger(“01234”)
Strings to IntegersInteger(“01234”)              => 668
Strings to Integers
Strings to IntegersInteger(“0x123”)
Strings to IntegersInteger(“0x123”)              => 291
Strings to Integers
Strings to IntegersInteger(“0b101”)
Strings to IntegersInteger(“0b101”)              => 5
YOU JUST NEED A FLOATING POINT?RUBY CAN DO THAT.
Integer to Float
Integer to FloatFloat(123)
Integer to FloatFloat(123)             => 123.0
Octal to Float
Octal to FloatFloat(01234)
Octal to FloatFloat(01234)               => 668.0
Hexadecimal to Float
Hexadecimal to FloatFloat(0x1234)
Hexadecimal to FloatFloat(0x1234)                => 291.0
Binary to Float
Binary to FloatFloat(0b101)
Binary to FloatFloat(0b101)               => 5.0
Strings to Floats
Strings to Floats       Works the same as the        non-Strings, just like            for Integer().
YOU JUST NEED A CHARACTER CODE?RUBY CAN DO THAT, TOO.
ASCII Code to Integer
ASCII Code to IntegerInteger(?a)
ASCII Code to IntegerInteger(?a)  ‘?’? Yep.
ASCII Code to IntegerInteger(?a)  ‘?’? Yep.              => 97
ASCII Code to Float
ASCII Code to FloatFloat(?A)
ASCII Code to FloatFloat(?A)            => 65.0
ASCII Code to FloatFloat(?A)                      => 65.0       String#getbyte(int) in Ruby 1.9         can also be used f...
YOU JUST NEED SOMETHING ELSE?RUBY CAN PROBABLY DO THAT, TOO.
Other Float Operations
Other Float Operations123.45.to_i    or to_int   => 123123.45.truncate            => 123123.45.ceil                => 1241...
Some String Operations
Some String Operations“123.45”.to_f        => 123.45“123.45”.to_i            => 123“101”.to_i               => 101“101”.to...
String Corner Cases
String Corner Cases“123.4a5”.to_f   => 123.4“12a3.45”.to_f    => 12.0“a123.45”.to_f     => 0.0“12a3.45”.to_i     => 12“a”....
Questions? Pic Credits! http://www.flickr.com/photos/jamescridland/2272701122/ http://www.flickr.com/photos/koenvereeken/208...
Number Conversions (MOTM 2010.12)
Upcoming SlideShare
Loading in...5
×

Number Conversions (MOTM 2010.12)

826

Published on

Ruby Method of the Month 2010.12. Lightning talk for the Columbus Ruby Brigade.

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
826
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Transcript of "Number Conversions (MOTM 2010.12)"

    1. 1. Number ConversionsRuby Method of the MonthKevin Munc - @muncman
    2. 2. YOU JUST NEED AN INTEGER?RUBY CAN DO THAT.
    3. 3. Float to Integer
    4. 4. Float to IntegerInteger(123.45)
    5. 5. Float to IntegerInteger(123.45) => 123
    6. 6. Float to Integer
    7. 7. Float to IntegerInteger(999.99)
    8. 8. Float to IntegerInteger(999.99) => 999
    9. 9. Float to IntegerInteger(999.99) No rounding => 999
    10. 10. Octal to Integer
    11. 11. Octal to IntegerInteger(01234)
    12. 12. Octal to IntegerInteger(01234) => 668
    13. 13. Hexadecimal to Integer
    14. 14. Hexadecimal to IntegerInteger(0x1234)
    15. 15. Hexadecimal to IntegerInteger(0x1234) => 291
    16. 16. Binary to Integer
    17. 17. Binary to IntegerInteger(0b101)
    18. 18. Binary to IntegerInteger(0b101) ‘b’ for binary
    19. 19. Binary to IntegerInteger(0b101) ‘b’ for binary => 5
    20. 20. Strings to Integers
    21. 21. Strings to IntegersInteger(“1234”)
    22. 22. Strings to IntegersInteger(“1234”) => 1234
    23. 23. Strings to Integers
    24. 24. Strings to IntegersInteger(“01234”)
    25. 25. Strings to IntegersInteger(“01234”) => 668
    26. 26. Strings to Integers
    27. 27. Strings to IntegersInteger(“0x123”)
    28. 28. Strings to IntegersInteger(“0x123”) => 291
    29. 29. Strings to Integers
    30. 30. Strings to IntegersInteger(“0b101”)
    31. 31. Strings to IntegersInteger(“0b101”) => 5
    32. 32. YOU JUST NEED A FLOATING POINT?RUBY CAN DO THAT.
    33. 33. Integer to Float
    34. 34. Integer to FloatFloat(123)
    35. 35. Integer to FloatFloat(123) => 123.0
    36. 36. Octal to Float
    37. 37. Octal to FloatFloat(01234)
    38. 38. Octal to FloatFloat(01234) => 668.0
    39. 39. Hexadecimal to Float
    40. 40. Hexadecimal to FloatFloat(0x1234)
    41. 41. Hexadecimal to FloatFloat(0x1234) => 291.0
    42. 42. Binary to Float
    43. 43. Binary to FloatFloat(0b101)
    44. 44. Binary to FloatFloat(0b101) => 5.0
    45. 45. Strings to Floats
    46. 46. Strings to Floats Works the same as the non-Strings, just like for Integer().
    47. 47. YOU JUST NEED A CHARACTER CODE?RUBY CAN DO THAT, TOO.
    48. 48. ASCII Code to Integer
    49. 49. ASCII Code to IntegerInteger(?a)
    50. 50. ASCII Code to IntegerInteger(?a) ‘?’? Yep.
    51. 51. ASCII Code to IntegerInteger(?a) ‘?’? Yep. => 97
    52. 52. ASCII Code to Float
    53. 53. ASCII Code to FloatFloat(?A)
    54. 54. ASCII Code to FloatFloat(?A) => 65.0
    55. 55. ASCII Code to FloatFloat(?A) => 65.0 String#getbyte(int) in Ruby 1.9 can also be used for this.
    56. 56. YOU JUST NEED SOMETHING ELSE?RUBY CAN PROBABLY DO THAT, TOO.
    57. 57. Other Float Operations
    58. 58. Other Float Operations123.45.to_i or to_int => 123123.45.truncate => 123123.45.ceil => 124123.45.floor => 123123.45.round => 123123.55.round .50 rounds up => 124
    59. 59. Some String Operations
    60. 60. Some String Operations“123.45”.to_f => 123.45“123.45”.to_i => 123“101”.to_i => 101“101”.to_i(16) base 16 => 257“101”.to_i(8) => 65“101”.to_i(2) => 5
    61. 61. String Corner Cases
    62. 62. String Corner Cases“123.4a5”.to_f => 123.4“12a3.45”.to_f => 12.0“a123.45”.to_f => 0.0“12a3.45”.to_i => 12“a”.to_i(16) => 10“010”.to_i(8) => 8
    63. 63. Questions? Pic Credits! http://www.flickr.com/photos/jamescridland/2272701122/ http://www.flickr.com/photos/koenvereeken/2088902012/ http://www.flickr.com/photos/pinksherbet/3041510366/ http://www.flickr.com/photos/stewf/2026818238/ http://www.flickr.com/photos/enigmatic/2431902112/ http://www.flickr.com/photos/alpower/282507392/SEE HTTP://WWW.SLIDESHARE.NET/MUNCMAN/ FOR OLD MOTMS.
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×