- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Haskell Program to Round a Number to n Decimal Places
In Haskell, we can use round, printf and truncate functions to round a number to n decimal places. In the first example, we are going to use (roundTo n x = (fromInteger $ round $ x * (10^n)) / (10.0^^n)) function and in the second example, we are going to use (roundTo n x = read $ printf ("%." ++ show n ++ "f") x) function.In the third example, we are going to use (roundTo n x = fromIntegral (truncate $ x * 10^n) / 10^n).
Algorithm
Step 1 − The roundTo function is defined using round function
Step 2 − 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 3 − The variable named, ‘x’ is defined that will hold the decimal number value that is to be rounded to n decimal places.
Step 4 − The roundTo function is called and the number and up to which decimal place it should be rounded of is passed as argument to it.
Step 5 − The resultant rounded number value up to n decimal places is printed to the console, once the function is being called.
Example 1
In this example, Number is rounded to n decimal places using round function.
roundTo :: Int -> Double -> Double roundTo n x = (fromInteger $ round $ x * (10^n)) / (10.0^^n) main :: IO () main = do let x = 3.14159265358979323846264338327950288419716939937510 let rounded = roundTo 4 x putStrLn $ "Original number: " ++ show x putStrLn $ "Number rounded to 4 decimal places: " ++ show rounded
Output
[1 of 1] Compiling Main ( main.hs, main.o ) Linking main ... Original number: 3.141592653589793 Number rounded to 4 decimal places: 3.1416
Example 2
In this example, Number is rounded to n decimal places using printf function.
import Text.Printf (printf) roundTo :: Int -> Double -> Double roundTo n x = read $ printf ("%." ++ show n ++ "f") x main :: IO () main = do let x = 3.14159265358979323846264338327950288419716939937510 let rounded = roundTo 4 x putStrLn $ "Original number: " ++ show x putStrLn $ "Number rounded to 4 decimal places: " ++ show rounded
Output
[1 of 1] Compiling Main ( main.hs, main.o ) Linking main ... Original number: 3.141592653589793 Number rounded to 4 decimal places: 3.1416
Example 3
In this example, Number is rounded to n decimal places using truncate function.
roundTo :: Int -> Double -> Double roundTo n x = fromIntegral (truncate $ x * 10^n) / 10^n main :: IO () main = do let x = 3.14159265358979323846264338327950288419716939937510 let rounded = roundTo 4 x putStrLn $ "Original number: " ++ show x putStrLn $ "Number rounded to 4 decimal places: " ++ show rounded
Output
[1 of 1] Compiling Main ( main.hs, main.o ) Linking main ... Original number: 3.141592653589793 Number rounded to 4 decimal places: 3.1415
Conclusion
Rounding a number to n decimal places in Haskell means that you want to round a given number to a specific number of decimal places, where n is the number of decimal places you want to round to. We can implement this by using round, printf and truncate function.