How do I check for a decimal point when checking a string is a valid number?
What I am thinking is that I use something like the following, but add code to check for
A simple approach involves using readMaybe
for converting a string into a number,
import Text.Read
and so for checking whether it is a Double
,
readMaybe "123" :: Maybe Double
Just 123.0
readMaybe "12a3" :: Maybe Double
Nothing
The latter returns Nothing
, the string is not a valid number. In a similar fashion, if we assume it is an Int
,
readMaybe "12.3" :: Maybe Int
Nothing