In diesem Tutorial werden wir verschiedene Methoden aus der Ruby-Bibliothek untersuchen, um einen String-Datentyp in einen numerischen Wert wie int, float oder rationale und komplexe Zahlen zu konvertieren.
You will often come across situations where you need to cast a string data type to a numeric value. For example, you need to convert user input to a numeric value before performing numeric operations or storing them in a database.
Method 1 - Convert string to int using to_i method
Ruby provides a simple method called to_i that converts a string value to an integer. Passing the target string to the function returns its integer equivalent, or 0 if the input string cannot be converted to an integer.
example code:
irb(main):002:0> var = "100"
=> "100"
irb(main):003:0> var.to_i
=> 100
The above example shows how the to_i function in Ruby can be used to convert a string to an integer.
If the input string cannot be converted to an integer, the function returns 0 as shown:
=> "geekbits"
irb(main):005:0> var.to_i
=> 0
If a string contains alphanumeric characters, the function interprets the leading numeric similar characters as int and ignores the rest.
Example for demonstration.
irb(main):006:0> var = '123hello'
=> "123hello"
irb(main):007:0> var.to_i
=> 123
As you can see from the output above, the function takes the leading numeric characters and ignores the non-numeric values.
NOTE: This function is only applied when the input string contains numeric values as leading characters. If the numeric values come after the alphabetic characters, the function returns 0 as shown:
irb(main):012:0> var = "hallo123"
=> "hallo123"
irb(main):013:0> var.to_i
=> 0
Convert Ruby String to Float
We can also convert a string to a floating point number using the to_f function. The function behaves similarly to the to_i function, except that it returns the value as a float.
Example:
irb(main):008:0> var = "3.14159"
=> "3.14159"
irb(main):009:0> var.to_f
=> 3.1415
Running the to_f function with a non-numeric string.
irb(main):010:0> var = "geekbits"
=> "geekbits"
irb(main):011:0> var.to_f
=> 0.0
In this case, the function returns a float value of 0.0.
Convert Ruby String to Big Decimal
We can also convert a string to BigDecimal type using the to_d function. Example:
irb(main):015:0> require 'bigdecimal'
=> wahr
irb(main):016:0> require 'bigdecimal/util'
=> wahr
irb(main):017:0> var = "100.232"
=> "100.232"
irb(main):018:0> var.to_d
=> 0.100232e3
For the to_d function, you need to import the bigdecimal and big decimal/util modules.
Convert ruby string to rational numbers
To convert a string to a rational number, use the to_r function as shown:
irb(main):019:0> var = "-3.14159"
=> "-3.14159"
irb(main):020:0> var.to_r
=> (-314159/100000)
Andere Beispiele:
irb(main):021:0> '100/5'.to_r
=> (20/1)
irb(main):022:0> '1_234_567'.to_r
=> (1234567/1)
irb(main):023:0> '01 Dec 22'.to_r
=> (1/1)
irb(main):025:0> '01/12/22'.to_r
=> (1/12)
Leading spaces and redundant characters after the end of a valid number are ignored. An underscore can separate strings of digits. If there is no valid number at the beginning of the string, the function returns 0.
End...
In this tutorial, you learned how to use various Ruby methods to convert an input type to numeric data, eg B. Integers, floating point numbers, decimal numbers, rational numbers and complex numbers.