show numbers with thousands separator

This commit is contained in:
Simon Michael 2007-07-03 19:01:13 +00:00
parent 0445086286
commit a266a9b2e0
4 changed files with 23 additions and 9 deletions

View File

@ -50,12 +50,22 @@ parseAmount s = nullamt
showAmountRoundedOrZero :: Amount -> String
showAmountRoundedOrZero (Amount cur qty) =
let rounded = printf "%.2f" qty in
let rounded = punctuatethousands $ printf "%.2f" qty in
case rounded of
"0.00" -> "0"
"-0.00" -> "0"
otherwise -> (symbol cur) ++ rounded
punctuatethousands :: String -> String
punctuatethousands s =
sign ++ (punctuate int) ++ frac
where
(sign,num) = break isDigit s
(int,frac) = break (=='.') num
punctuate = reverse . concat . intersperse "," . triples . reverse
triples "" = []
triples s = [take 3 s] ++ (triples $ drop 3 s)
instance Num Amount where
abs (Amount c q) = Amount c (abs q)
signum (Amount c q) = Amount c (signum q)

1
NOTES
View File

@ -3,7 +3,6 @@ hledger project notes
* TO DO
** bugs
** compatibility
*** , in thousands
*** use greatest precision in register
*** abbreviate 0
*** don't combine entries so much in register

View File

@ -294,13 +294,16 @@ quickcheck = mapM quickCheck ([
] :: [Bool])
hunit = runTestTT $ "hunit" ~: test ([
"" ~: parseLedgerPatternArgs [] @=? ([],[])
,"" ~: parseLedgerPatternArgs ["a"] @=? (["a"],[])
,"" ~: parseLedgerPatternArgs ["a","b"] @=? (["a","b"],[])
,"" ~: parseLedgerPatternArgs ["a","b","--"] @=? (["a","b"],[])
,"" ~: parseLedgerPatternArgs ["a","b","--","c","b"] @=? (["a","b"],["c","b"])
,"" ~: parseLedgerPatternArgs ["--","c"] @=? ([],["c"])
,"" ~: parseLedgerPatternArgs ["--"] @=? ([],[])
"" ~: parseLedgerPatternArgs [] @?= ([],[])
,"" ~: parseLedgerPatternArgs ["a"] @?= (["a"],[])
,"" ~: parseLedgerPatternArgs ["a","b"] @?= (["a","b"],[])
,"" ~: parseLedgerPatternArgs ["a","b","--"] @?= (["a","b"],[])
,"" ~: parseLedgerPatternArgs ["a","b","--","c","b"] @?= (["a","b"],["c","b"])
,"" ~: parseLedgerPatternArgs ["--","c"] @?= ([],["c"])
,"" ~: parseLedgerPatternArgs ["--"] @?= ([],[])
,"" ~: punctuatethousands "" @?= ""
,"" ~: punctuatethousands "1234567.8901" @?= "1,234,567.8901"
,"" ~: punctuatethousands "-100" @?= "-100"
,"" ~: test_ledgertransaction
,"" ~: test_ledgerentry
,"" ~: test_autofillEntry

View File

@ -1,6 +1,7 @@
-- standard imports and utilities
module Utils (
module Utils,
module Char,
module Data.List,
module Data.Tree,
module Text.Printf,
@ -10,6 +11,7 @@ module Utils (
module Test.HUnit
)
where
import Char
import Data.List
import Data.Tree
import Text.Printf