Rounding to 2 decimal places

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Rounding to 2 decimal places

Raghav Malik
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Rounding to 2 decimal places

David McBride
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 <[hidden email]> 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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Rounding to 2 decimal places

Imants Cekusins

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners