[Haskell-cafe] round function
J. Garrett Morris
trevion at gmail.com
Sun Feb 12 18:28:40 EST 2006
I think the function you're looking for is:
myRound n places = round (n / fromIntegral factor) * factor
where factor = 10 ^ (places - 1)
In this case, 10 ^ (places - 1) has integral type (either Int or
Integer). I need it to be a fractional type to divide n by it, so I
use fromIntegral to convert it.
On 2/12/06, Chatzianastassiou Achilleas <achatzianastassiou at hotmail.com> wrote:
> Thanks for the anwser Sebastian,
> this seems to work, however I am trying to implement something like
> Main> myfunction 123.456 2
> >From: Sebastian Sylvan <sebastian.sylvan at gmail.com>
> >Reply-To: sylvan at student.chalmers.se
> >To: Chatzianastassiou Achilleas <achatzianastassiou at hotmail.com>
> >CC: haskell-cafe at haskell.org
> >Subject: Re: [Haskell-cafe] round function
> >Date: Sun, 12 Feb 2006 22:23:17 +0100
> >On 2/12/06, Chatzianastassiou Achilleas <achatzianastassiou at hotmail.com>
> > > Hi all,
> > >
> > > I am trying to implement a function that finds the significant figure of
> > > number to a specified point i.e. 2.55555 3 = 2.556. I have implemented
> > > something like:
> > >
> > > sig :: (RealFrac a, Integral b) => a -> Int -> a
> > > sig x y = round y
> > >
> > > However this doesn't work. Moreover at the Prelude when i type round
> > > 2 i get an error.
> > > Any suggestions?
> > >
> >Something like:
> >myround n s = fromIntegral (round (n * factor)) / factor
> > where factor = fromIntegral (10^s)
> >Basically scaling it to bring the specified number of digits to the
> >left of the decimal point, then rounding, then scaling back.
> >Sebastian Sylvan
> >UIN: 44640862
> FREE pop-up blocking with the new MSN Toolbar – get it now!
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
We have lingered in the chambers of the sea
By sea-girls wreathed with seaweed red and brown
Till human voices wake us, and we drown.
More information about the Haskell-Cafe