[Haskell-beginners] Rounding to 2 decimal places

David McBride toad3k at gmail.com
Tue Mar 27 16:32:56 UTC 2018


Because there is already a Show instance for Complex a, you will have to
use your own show function.  Something list this.

import Text.Printf
import Data.Complex

showComplex :: Complex Float -> String
showComplex (a :+ b) = (printf "%.2f" a) ++ " :+ " ++ (printf "%.2f" b)


On Tue, Mar 27, 2018 at 12:11 PM, Raghav Malik <malikraghav at gmail.com>
wrote:

> Hi,
>
> I have a complex double (x :+ y) where 'x' and 'y' are computed using sin
> and cos functions on an angle expressed in radians.
>
> However, the output on the command line (using GHCi) shows up with a lot
> of decimal places. How can I limit the values of 'x' and 'y' to having only
> two decimal places?
>
> Thanks,
> Raghav
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180327/b3b2983b/attachment.html>


More information about the Beginners mailing list