tags:

views:

143

answers:

2

Is there a library function to put commas into numbers with Haskell? I can't find one for the life of me.

I want a function that would work something like this:

format 1000000 = "1,000,000"
format 1045.31 = "1,045.31"

but I can't seem to find any number formatting functions of this type in Haskell. Where are the number formatting functions?

A: 

Text.Printf

http://www.haskell.org/ghc/docs/6.12.1/html/libraries/base/Text-Printf.html

Raghs
I'm not sure this works for what is requested. This simply provides `printf`, but what implementation of `printf` supports regional numeric display with commas? What he wants is something like [Number::Format](http://search.cpan.org/dist/Number-Format/Format.pm) for perl.
Evan Carroll
+2  A: 

Perhaps you could use some of the functions from Data.Split:

http://hackage.haskell.org/cgi-bin/hackage-scripts/package/split

I know this isn't quite what you want, but you could use Data.List.intersperse

http://haskell.org/ghc/docs/6.12.1/html/libraries/base-4.2.0.0/Data-List.html#v:intersperse

EDIT: This does what you want, although I know you want a library function, this may be as close as you get (please excuse my poor coding style):

import Data.List.Split
import Data.List

format x = h++t
    where
        sp = break (== '.') $ show x
        h = reverse (intercalate "," $ splitEvery 3 $ reverse $ fst sp) 
        t = snd sp
Jonno_FTW
This was actually my thinking as well.
Evan Carroll