[Haskell-beginners] Show Floats
Nathan M. Holden
nathanmholden at gmail.com
Fri Nov 6 00:31:05 EST 2009
I have been working on a small library that will typeset notes in LaTeX for
me, since I tend to have haphazard typesetting while I write, but while I read
I like to have standards.
Anyways, I defined a datatype
data Color = RGB {
name :: [Char],
r :: Float,
g :: Float,
b :: Float,
matchText :: [[Char]],
targetText :: [Char]}
deriving(Show,Eq,Read)
I wanted to be able to have a piece of code that said
"\\definecolor{"++name++"}{rgb}{"++show r++","++show g++","++show
b++"}"
but because I have numbers below 0.1, it outputs as 2.0e-2, which is
useless. I wrote a function that would output useful numbers, but it's REALLY
bad Haskell:
fToInt :: Float -> Int
fToInt f = if f >= 10 then fToInt (f-10.0)
else if (f >= 9) then 9
else if (f >= 8) then 8
else if (f >= 7) then 7
else if (f >= 6) then 6
else if (f >= 5) then 5
else if (f >= 4) then 4
else if (f >= 3) then 3
else if (f >= 2) then 2
else if (f >= 1) then 1 else 0
It takes up 11 lines in a module that's only got 74! (128 if you count the
module to translate the notes into a .tex file)
how would I write this better?
More information about the Beginners
mailing list