
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert String to Floating Point Number in Haskell
This article will help us in converting the string into a floating point number. In Haskell, you can use the read function to convert a string to a floating-point number. Another way to convert a string to a floating-point number is by using the readMaybe function. This function is similar to the read function, but it returns a Maybe value instead of raising an exception when the input string is not valid.
All these functions are part of the standard library and we can use them to easily convert strings to a floating point number in Haskell.
Algorithm
Step 1 ? stringToFloat() function is defined using read function.
Step 2 ? Program execution will be started from main function. The main() function has whole control of the program.
Step 3 ? A variable named, "value" is being initialized. It will have the string value that is to be converted into a floating point number.
Step 4 ? stringToFloat() function is being called and string will be passed as parameter to it.
Step 5 ? Final resultant converted floating point number value is displayed.
Method 1: Using read function
This approach uses the read function which converts a string to any type that is an instance of the Read typeclass. It's a simple function which doesn't check if the string can be parsed successfully, however, if the string is not a valid representation of a float it will raise an exception.
Example
In this example, we are going to see that how we can convert the string into an integer using read function.
stringToFloat :: String -> Float stringToFloat s = read s main :: IO () main = do let value = "3.14" let floatValue = stringToFloat value print floatValue
Output
3.14
Method 2: Using readMaybe function
In this example, we are going to see that how we can convert the string into an integer using readMaybe function. This approach uses the readMaybe function in a case expression to check if the string can be successfully parsed to a Float. If it can, the resulting Just value is returned, otherwise the expression returns an error message "Invalid float value".
Example
import Text.Read (readMaybe) stringToFloat :: String -> Float stringToFloat s = case readMaybe s of Just x -> x Nothing -> error "Invalid float value" main :: IO () main = do let value = "3.14" let floatValue = stringToFloat value print floatValue
Output
3.14
Method 3: Using realToFrac function
In this example, the stringToFloat function takes a string as an argument and uses readMaybe to attempt to parse the string as a double precision floating-point number. The fmap function is used to apply the realToFrac function to the double precision floating-point number. realToFrac converts a Real to a Fractional. The function returns a Maybe Float which would contain Just 3.14 if the conversion succeeded or Nothing if the conversion failed.
Example
import Text.Read (readMaybe) stringToFloat :: String -> Maybe Float stringToFloat s = fmap realToFrac (readMaybe s :: Maybe Double) main :: IO () main = do let value = "3.14" let floatValue = stringToFloat value print floatValue
Output
Just 3.14
Conclusion
In Haskell, the process of converting a string to a floating point number typically involves using the read function or readMaybe function from the Text.Read module.
Another approach is to use the readFloat function from the Data.ByteString module. This approach is useful in haskell, when we want to parse a string and also keep track of the remaining input after the float value.