4 ms·
Yeah, now i can confidently use this to check a string is float def is_float?(fl) fl =~ /(^(\d+)(\.)?(\d+)?)|(^(\d+)?(\.)(\d+))/ end
by revskill 4y ago
Yeah, now i can confidently use this to check a string is float
def is_float?(fl)
fl =~ /(^(\d+)(\.)?(\d+)?)|(^(\d+)?(\.)(\d+))/
end
- geysersam 4y agoForgot the minus sign! Doesn't Ruby allow scientific notation for floats?
- camgunz 4y agoFloats are pretty hard, especially if you're accepting exponentiation: - 1.4 - -1.4 - .4 - -.4 - 1. - -1. ... plus exponents, also you don't need a decimal point if there's an exponent Won't Ruby parse a string into a float? I would just use that and check for an error unless your requirements are different.
- Rafert 4y ago`Float(str, exception: false)` returns either float or nil.
- pwdisswordfish9 4y agoWhat does that pattern match that the following simplification doesn't? /(^(\d+)(\.)?(\d+)?)|(^(\.)(\d+))/