Rounding formula for automatic transactions

148 views
Skip to first unread message

André Coelho

unread,
Jan 6, 2012, 2:25:09 PM1/6/12
to ledge...@googlegroups.com
Hello,

Is it possible to specify the rounding formula in a automatic transaction?  My specific case is that of tithe, I know I might be nitpicking, but still, I think it would be useful to have a way to say, always round up in the X decimal place, or always round down.  Here is an example:

Let's say I have a paycheck of 1234.34, having a rule like:

= /^Income/
  (Liabilities:Tithe)  0.1

Would produce something like this:

1/5 Employer Name
  Income:Salary
  Assets:Checking  $1234.34
  (Liabilities:Tithe)   $123.43

But it would be nice to be able to say, always round up the second decimal place, which would produce 123.44.  Is this currently possible?  If not, do you think that by using a rule like 0.10001 would be accurate?  I noticed that indeed it produces 123.44, but then the balance is missing 0.01 when I make a payment of 123.44 to that account.  So somehow this math is not quite accurate.

Any ideas?  Thank you.

Andre.

Zack Williams

unread,
Jan 6, 2012, 3:35:13 PM1/6/12
to ledge...@googlegroups.com
> Is it possible to specify the rounding formula in a automatic transaction?
>  My specific case is that of tithe, I know I might be nitpicking, but still,
> I think it would be useful to have a way to say, always round up in the X
> decimal place, or always round down.  Here is an example:

I get similar results with payroll automatic transactions and
partial-cents rounding up or down after a few pay periods.

While it isn't "tidy", over the course of multiple transactions it is
more accurate, as these fractions of a cent may eventually accumulate
to a whole cent, and a rounding formula would likely throw this off.

(Insert Superman 2/Office Space joke here)

- Zack

Craig Earls

unread,
Jan 6, 2012, 4:27:42 PM1/6/12
to ledge...@googlegroups.com
The best way is to use a value expression in your automatic transaction:

= /^Income/
(Liabilities:Tithe) (0.1 * round(amount))


Unfortunately, round doesn't seem to let you control the precision so
you won't notice much since it will round to two digits.

I have another request to allow user controlled precision, but maybe
John has a different method.

--
Craig, Corona De Tucson, AZ
enderw88.wordpress.com

André Coelho

unread,
Jan 6, 2012, 6:13:13 PM1/6/12
to ledge...@googlegroups.com
I've seen a floor function, but it seems to actually round to the nearest integer (not a floor as I'm used to).  I wonder how do you manage precision, I would think this should be critical, how do you guys deal with that?

Andre.

Craig Earls

unread,
Jan 6, 2012, 6:39:39 PM1/6/12
to ledge...@googlegroups.com
Precision is handled internally to ledger. It keeps as many digits as
the most precise number it has gotten for that commodity. As an
experiment, enter four significant figures for you primary commodity
somewhere in any posting (for example $13.4500 instead of $13.45).
Your balance reports will all show 4 significant figures. There is no
external way to control to this that I know of.

2012/1/6 André Coelho <andre...@gmail.com>:

--

Craig Earls

unread,
Jan 6, 2012, 6:50:28 PM1/6/12
to ledge...@googlegroups.com
Actually, after further thought this is not as accurate as you might
want. If you are looking to calculate 0.1 of your total increase
rounded to the nearest dollar that is different than summing the
rounding of all the individual increases. Ideally you would apply the
rounding on the reporting end, which is easy to do:

16:46:55 ~/FinanceData (master) > ledger bal 'Tithe Owed'
$ -1.75 Liabilities:Tithe Owed
16:47:11 ~/FinanceData (master) > ledger bal 'Tithe Owed' --format
"%10(floor(amount)) %20(account)\n"
$ -2.00 Liabilities:Tithe Owed

Which shows that I am slightly ahead on my tithe...

Of course it is much harder if you was a consolidated report of lots
of accounts, with only the Tithe Owed account rounded in this
fashion...it is possible, just harder...let me know if you want to see
a format string that could do that...

Craig

Craig Earls

unread,
Jan 6, 2012, 9:49:55 PM1/6/12
to ledge...@googlegroups.com
Here is the format string I mentioned. This is for the cleared
report. It colorizes lines whose cleared and uncleared balances
aren't equal, and uses the floor function on any account with "Owed"
in the name. For me that means "Tithe Owed".

--format "%(ansify_if(
justify(depth_spacer + partial_account(options.flat), 18,
0, false, color) +
justify( ( account =~ /Owed/ ?
floor(scrub(get_at(total_expr, 0))) :
scrub(get_at(total_expr,
0))), 16, 16 + prepend_width, true, color) +
justify( ( account =~ /Owed/ ?
floor(scrub(get_at(total_expr, 1))) :
scrub(get_at(total_expr,
1))), 18, 36 + prepend_width, true, color) + ' ' +
(latest_cleared ? format_date(latest_cleared) : \" \"),
( get_at(total_expr, 1) == get_at( total_expr, 0))
? green : yellow ) )
%/ ------------ ------------
%34(get_at(display_total, 0)) %17(get_at(display_total, 1))"

John Wiegley

unread,
Jan 6, 2012, 10:19:50 PM1/6/12
to ledge...@googlegroups.com
>>>>> Craig Earls <enderw88-Re5JQE...@public.gmane.org> writes:

> Unfortunately, round doesn't seem to let you control the precision so you
> won't notice much since it will round to two digits.
>
> I have another request to allow user controlled precision, but maybe John
> has a different method.

You can use (0.1 * unround(amount)), to turn rounding off. But currently
there is not a way to set the rounding precision for a given value. However,
that would be really easy to do, so that's a great feature idea.

John

John Wiegley

unread,
Jan 7, 2012, 1:31:19 AM1/7/12
to ledge...@googlegroups.com
>>>>> André Coelho <andrecoelho-Re5JQ...@public.gmane.org> writes:

Yeah, floor has a bug, and their needs to be a ceiling too. That one is known
at least. :)

John

André Coelho

unread,
Jan 11, 2012, 6:18:50 PM1/11/12
to ledge...@googlegroups.com
Good to know it is a bug.  It would definitely be great to have these functions and precision control for the more paranoid of us.  Thanks for sharing such an awesome tool :D.

Andre.
Reply all
Reply to author
Forward
0 new messages