From a266a9b2e0e0f4519dd3f75799671f8624358047 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 3 Jul 2007 19:01:13 +0000 Subject: [PATCH] show numbers with thousands separator --- Amount.hs | 12 +++++++++++- NOTES | 1 - Tests.hs | 17 ++++++++++------- Utils.hs | 2 ++ 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/Amount.hs b/Amount.hs index 17bc369be..91ae65db5 100644 --- a/Amount.hs +++ b/Amount.hs @@ -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) diff --git a/NOTES b/NOTES index f26898985..6b537c9fe 100644 --- a/NOTES +++ b/NOTES @@ -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 diff --git a/Tests.hs b/Tests.hs index 5ce9f0a5a..c1d575582 100644 --- a/Tests.hs +++ b/Tests.hs @@ -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 diff --git a/Utils.hs b/Utils.hs index db0a720fb..bb20e4b20 100644 --- a/Utils.hs +++ b/Utils.hs @@ -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