# Fixed point decimals for accounting

**URL:** <https://discourse.julialang.org/t/fixed-point-decimals-for-accounting/63400>\
**Category:** General Usage\
**Created:** [June 22, 2021, 9:55pm UTC](https://discourse.julialang.org/t/fixed-point-decimals-for-accounting/63400 "2021-06-22T21:55:29Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [June 23, 2021, 9:29pm UTC](https://discourse.julialang.org/t/fixed-point-decimals-for-accounting/63400/21 "2021-06-23T21:29:12Z")

</div>

It’s not that the accounting rules are funky, so much as small errors in computing, are a problem. Basically, the numbers are always two decimal places, and work like integers.

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [June 23, 2021, 10:06pm UTC](https://discourse.julialang.org/t/fixed-point-decimals-for-accounting/63400/22 "2021-06-23T22:06:24Z")

</div>

Treating money as integer multiples of $0.01 can be exact if you only use +, -, and integer multiplication, but not if you allow division (splitting a dinner bill three ways) or multiplication by non-unit ratios (converting between currencies).

There must be standard practices for such cases in accounting. I’d think they’d be worked out as carefully as IEEE floating-point standards! I know nothing about it, but I have noticed that the IRS rounds to the nearest dollar.

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [June 23, 2021, 10:13pm UTC](https://discourse.julialang.org/t/fixed-point-decimals-for-accounting/63400/23 "2021-06-23T22:13:39Z")

</div>

That’s a good argument to implement a custom type: That way you can prevent division by either leaving it unimplemented or implementing it to immediatly raise an exception

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 23, 2021, 11:01pm UTC](https://discourse.julialang.org/t/fixed-point-decimals-for-accounting/63400/24 "2021-06-23T23:01:48Z")

</div>

> [@brett\_knoss](#):
>
> It might not be an issue to just use floats, but the problem, is that I’m worried that some of the tests won’t work right, if the difference between certain values is not exactly 0.

Realize that decimal floating point is basically just as exact as decimal fixed point, with a much greater dynamic range.

Any elementary operation (`+,*,/,-`) whose result _can_ be exactly represented in decimal _is_ computed exactly, until you exceed the available number of significand digits. And when you do exceed the precision, the roundoff errors are typically much more gradual and benign than in fixed point (where overflow is catastrophic).

There is a pernicious myth that floating-point arithmetic always involves “random errors,” as if `1.0+1.0` only approximately gives 2.0.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [June 23, 2021, 11:03pm UTC](https://discourse.julialang.org/t/fixed-point-decimals-for-accounting/63400/25 "2021-06-23T23:03:19Z")

</div>

So, rounding to two decimals should remove any error.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 23, 2021, 11:04pm UTC](https://discourse.julialang.org/t/fixed-point-decimals-for-accounting/63400/26 "2021-06-23T23:04:25Z")

</div>

Rounding generally increases error, almost by definition.

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [June 23, 2021, 11:05pm UTC](https://discourse.julialang.org/t/fixed-point-decimals-for-accounting/63400/27 "2021-06-23T23:05:17Z")

</div>

[Decimals.jl](https://juliahub.com/ui/Packages/Decimals/04Vnf/0.4.1) says

> Unlike another Julia package called [`DecFP`](https://github.com/JuliaMath/DecFP.jl), which aims at implementing the [IEEE 754-2008 standard](https://en.wikipedia.org/wiki/IEEE_754-2008_revision) introducing 32, 64, and 64-bit precisions ([decimal32](https://en.wikipedia.org/wiki/Decimal32_floating-point_format), [decimal64](https://en.wikipedia.org/wiki/Decimal64_floating-point_format) and [decimal128](https://en.wikipedia.org/wiki/Decimal128_floating-point_format), respectively), this package allows arbitrary precision. Note, however, that in comparision with `DecFP` , which is is essentially a wrapper for a specialized [Intel® Decimal Floating-Point Math Library](https://software.intel.com/en-us/articles/intel-decimal-floating-point-math-library), the present package is more computationally demanding. If more computational efficiency is demanded, functions from [`libmpdec`](http://www.bytereef.org/mpdecimal/index.html) library can be called directly.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 23, 2021, 11:07pm UTC](https://discourse.julialang.org/t/fixed-point-decimals-for-accounting/63400/28 "2021-06-23T23:07:12Z")

</div>

Note that “arbitrary” precision is not the same as “infinite” precision. For example, `1/3` will never be represented exactly in decimal floating point regardless of the precision.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [June 23, 2021, 11:07pm UTC](https://discourse.julialang.org/t/fixed-point-decimals-for-accounting/63400/29 "2021-06-23T23:07:51Z")

</div>

If you work with 100x the dollar value (ie. work with pennies) in **normal binary floating point** and round to the nearest integer you’ll probably do just fine unless maybe you’re working with transactions between the Federal Reserve and the US Treasury or something (ie. trillions of dollars)

> [@stevengj](#):
>
> Rounding generally increases error

Unless you’re talking about a situation like where the answer is person A should pay person B 33.33333333333333 dollars but because there is no 1/3 dollar they pay exactly $33.33 and then rounding to the nearest penny gives the **correct answer** to the question “what actually occurred?”

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [June 23, 2021, 11:24pm UTC](https://discourse.julialang.org/t/fixed-point-decimals-for-accounting/63400/30 "2021-06-23T23:24:51Z")

</div>

Exactly, and forget the floor stuff, I’m not sure what I was thinking there, normal rounding is always used.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [June 24, 2021, 4:21pm UTC](https://discourse.julialang.org/t/fixed-point-decimals-for-accounting/63400/31 "2021-06-24T16:21:16Z")

</div>

I think rounding to two digits will work. Is there a way to always display two digits?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 24, 2021, 5:15pm UTC](https://discourse.julialang.org/t/fixed-point-decimals-for-accounting/63400/32 "2021-06-24T17:15:15Z")

</div>

> [@dlakelan](#):
>
> rounding to the nearest penny gives the **correct answer** to the question “what actually occurred?”

It’s my understanding that rounding to the nearest penny generally only occurs for the final transaction (e.g. when money exchanges hands, a bank deposit is made, etcetera), not necessarily on every intermediate financial calculation.

Also, in accounting situations you might have a fiduciary requirement to exactly represent decimal quantities, hence decimal fixed or floating point are often recommended over binary floating point. (Even if the precision of the binary floating point is such that $0.01 errors are unlikely to ever appear in the final rounded result no matter how many intermediate calculations were performed, you don’t want to be the a position of having to prove this by numerical analysis in a court.)

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [June 24, 2021, 5:52pm UTC](https://discourse.julialang.org/t/fixed-point-decimals-for-accounting/63400/33 "2021-06-24T17:52:16Z")

</div>

> [@stevengj](#):
>
> generally only occurs for the final transaction (e.g. when money exchanges hands, a bank deposit is made, etcetera), not necessarily on every intermediate financial calculation.

yeah, I think that’s right. There are probably all sorts of legal issues as you mention later as well. Decimal floating point would be a good choice.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 24, 2021, 5:57pm UTC](https://discourse.julialang.org/t/fixed-point-decimals-for-accounting/63400/34 "2021-06-24T17:57:12Z")

</div>

Of course, what’s really needed here is a numeric type that tracks the total roundoff error and automatically [deposits the “leftover money” into the programmer’s bank account](https://filmschoolrejects.com/getting-rich-with-richard-pryors-banking-scheme-from-superman-iii-7811840a8a0/).

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [June 24, 2021, 6:26pm UTC](https://discourse.julialang.org/t/fixed-point-decimals-for-accounting/63400/35 "2021-06-24T18:26:33Z")

</div>

I don’t think it would end up in court, and rounding should allow the tests to work. (I can see if I have trouble). The only thing is that it only shows 1 or 2 decimal places, do it looks wrong.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [June 24, 2021, 6:42pm UTC](https://discourse.julialang.org/t/fixed-point-decimals-for-accounting/63400/36 "2021-06-24T18:42:00Z")

</div>

Isn’t that the plot of Superman 3? Just make sure to not put a decimal in the wrong spot!

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [June 24, 2021, 7:11pm UTC](https://discourse.julialang.org/t/fixed-point-decimals-for-accounting/63400/37 "2021-06-24T19:11:36Z")

</div>

> [@brett\_knoss](#):
>
> The only thing is that it only shows 1 or 2 decimal places, do it looks wrong

printing with 2 decimal places is no big deal, assuming your value is in pennies:

```julia
julia> p = 3316.0
3316.0

julia> @printf "You have \$%.2f" p/100
You have $33.16

```

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [June 24, 2021, 7:53pm UTC](https://discourse.julialang.org/t/fixed-point-decimals-for-accounting/63400/38 "2021-06-24T19:53:06Z")

</div>

Is that using a package? What if I add to a dataframe instead of printing?

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [June 24, 2021, 7:57pm UTC](https://discourse.julialang.org/t/fixed-point-decimals-for-accounting/63400/39 "2021-06-24T19:57:35Z")

</div>

Yeah a built-in / standard library package [Printf · The Julia Language](https://docs.julialang.org/en/v1/stdlib/Printf/)

```julia
using Printf

```

> [@brett\_knoss](#):
>
> What if I add to a dataframe instead of printing?

If you’re trying to create an “accounting number” based on using floating point in pennies… then you’ll want to create your own struct subtype of number

```julia
struct AccountBalance <: Number
    bal::Float64
end

```

Then you’ll want to create a constructor that automatically takes the balance, multiplies it by 100 and rounds it… create some + and - functions and such, and a printing function that uses @printf to do the right thing.

```julia
julia> function print(x::AccountBalance)
       @printf "\$%.2f" x.bal/100.0
       end
print (generic function with 33 methods)

julia> print(foo)
$13.80

```

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [June 28, 2021, 7:08pm UTC](https://discourse.julialang.org/t/fixed-point-decimals-for-accounting/63400/40 "2021-06-28T19:08:48Z")

</div>

> [@dlakelan](#):
>
> ```julia
> function print(x::AccountBalance)
> @printf "\$%.2f" x.bal/100.0
> end
> 
> ```

Pluto doesn’t allow that @printf macro. It doesn’t allow a lot of macros, I think that’s something being worked on

[Previous page](https://discourse.julialang.org/t/fixed-point-decimals-for-accounting/63400.md?page=1)

[Next page](https://discourse.julialang.org/t/fixed-point-decimals-for-accounting/63400.md?page=3)
