如何在 Crystal 中将字符串转换为整数或浮点数?
How can I convert a String to an Integer or Float in Crystal?
在Crystal中,如何将String
转换为Integer
或Float
?
使用 Python 我可以简单地执行以下操作:
>>> nb = "123"
>>> int(nb)
123
>>> nb = "1.23"
>>> float(nb)
1.23
Crystal有没有类似的工具?
您可以使用 String#to_i
and String#to_f
方法:
"123".to_i # => 123
"123".to_i64 # => 123 as Int64
"1.23".to_f # => 1.23
"1.23".to_f64 # => 1.23 as Float64
等等
在Crystal中,如何将String
转换为Integer
或Float
?
使用 Python 我可以简单地执行以下操作:
>>> nb = "123"
>>> int(nb)
123
>>> nb = "1.23"
>>> float(nb)
1.23
Crystal有没有类似的工具?
您可以使用 String#to_i
and String#to_f
方法:
"123".to_i # => 123
"123".to_i64 # => 123 as Int64
"1.23".to_f # => 1.23
"1.23".to_f64 # => 1.23 as Float64
等等