
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 Double Type Variables to String in Haskell
In Haskell, we will convert double type variables into string by using user-defined function, doubleToString along with show function and Text.Printf and Data.Text module. In the first example, we are going to use ( let valueString = show input) and in the second example, we are going to use (doubleToString d = printf "%.2f" d). And in the third example, we are going to use (doubleToString d = unpack $ pack $ show d).
Algorithm
Step 1 ? The program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.
Step 2 ? The variable named, "input" is being initialized. It will hold the double value that is to be converted to respective string value.
Step 3 ? The show function is called with double value.
Step 4 ? The resultant string value is printed to the console using ?putStrLn' statement.
Example 1
In this method, the Double input is taken from the user, and show is used to convert that input to a String. The resulting String value is then printed using putStrLn.
main :: IO () main = do let input = 245.64 let valueString = show input putStrLn $ "The string value is: " ++ valueString
Output
The string value is: 245.64
Example 2
In this method, a function doubleToString is defined that uses the printf function to format the Double value as a string with two decimal places. We then use doubleToString to convert a Double value to a String and print it using putStrLn.
import Text.Printf (printf) doubleToString :: Double -> String doubleToString d = printf "%.2f" d main :: IO () main = do let value = 3.14159 :: Double putStrLn $ "The string value is: " ++ doubleToString value
Output
The string value is: 3.14
Example 3
In this method, the show function is used to convert the Double value to a String, and then use Data.Text.pack to convert the String to a Text value, and finally use Data.Text.unpack to convert the Text value back to a String.
import Data.Text (pack, unpack) doubleToString :: Double -> String doubleToString d = unpack $ pack $ show d main :: IO () main = do let value = 3.14159 :: Double putStrLn $ "The string value is: " ++ doubleToString value
Output
The string value is: 3.14159
Conclusion
Double to string conversion is the process of converting a double precision floating-point value (also known as a double) to a string representation.In Haskell, you can use the show function to convert a Double value to a string. The show function takes a value of a specified type, such as Double, and returns a string representation of that value. The resulting string can then be printed, stored in a file, or sent over a network. Also, we can Data.Text and Text.Printf module for this conversion.