Number ExamplesUse numbers and the zero and eql methods. Convert strings into Integers and Floats.
This page was last reviewed on Nov 16, 2022.
Numbers. Programs use numbers everywhere. Many numeric types and conversions can be done in Ruby. Many programs use the Integer and Float built-in methods.
Ruby examples. Methods like Float() and Integer() can perform simple conversions. Strings can be converted to numbers, and back again as well.
Integer, Float. Here we convert string data to number types. A string may contain digits but not be a number. With the Integer conversion, we convert it to one. We also use Float.
Here We convert the string "1234" into an Integer. And we convert a string with floating-point data into a Float.
Then We add the 2 numbers together. This shows they are no longer strings, which could not be added in this way.
# Two strings with numeric contents. value1 = "1234" value2 = "1234.5678" # Convert strings to numbers. number1 = Integer(value1) number2 = Float(value2) # Print numbers. print "Number1: ", number1, "\n" print "Number2: ", number2, "\n" # Add numbers together. # ... Could not be done with strings. number3 = number1 + number2 # Print final number. print "Number3: ", number3, "\n"
Number1: 1234 Number2: 1234.5678 Number3: 2468.5678
Exponents. We can directly use exponents, with the two-star operator. In this program, we raise a value to the power of two, and then to the power of three.
Tip More advanced mathematical operations are available as methods in the Math class.
# An initial value. value = 3 # Square the value. square = value ** 2 # Cube the value. cube = value ** 3 # Display our results. puts square puts cube
9 27
Zero, nonzero. Zero() returns true or false. If the number is zero, it returns true. Otherwise, it returns false. Nonzero meanwhile returns the original number if it is not zero.
And If the number is zero, nonzero returns nil. So it returns the number with 0 changed to nil.
value = 0 if value.zero? puts "A" end if value.nonzero? puts "B" # Not reached. end value = 1 if value.nonzero? puts "C" end
Eql operator. Unlike the "==" operator, eql compares types. So a floating point number, like 1.0, is not equal to an Integer number like 1. The == operator treats them as equal.
Tip Using the == operator is superior in most programs. The eql method just adds complexity—1.0 usually should equal 1.
value1 = 1 value2 = 1.0 value3 = 1 if value1.eql? value2 puts "A" # Not reached. end if value1 == value2 puts "B" end if value1.eql? value3 puts "C" end
Random numbers. With the rand method we generate a pseudo-random number. The srand method is used to seed our random number generator.
Math. Some numeric operations can be done directly, with operators like + or minus. But many math methods exist, included for easy access. These handle more complex things like sqrt.
A summary. Tasks that involve numbers are often language-specific. In Ruby, we have many helpful operators available on numbers. We convert and manipulate numbers with ease.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
© 2007-2022 sam allen.
see site info on the changelog.