Skip to content

Adaptive price base#2299

Open
upsuper wants to merge 3 commits into
beancount:mainfrom
upsuper-forks:adaptive-price-base
Open

Adaptive price base#2299
upsuper wants to merge 3 commits into
beancount:mainfrom
upsuper-forks:adaptive-price-base

Conversation

@upsuper

@upsuper upsuper commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

When there is a commodity conversion, in the price column it always shows the per unit price of the target commodity, e.g. if I sell 1 VOO for 695 USD, the price would show 695 USD. This is usually okay for shares where the counterpart commodity is the denominated currency, so the price is usually quite recognizable. However, it may not work well when both sides are fiat currencies and they have very different scale of value. As an example, when converting from USD to JPY or AUD to JPY, the price just shows as 0.01 USD or 0.01 AUD, which is not very useful to know the actual exchange rate involved.

This PR changes how price is displayed. It chooses an adaptive price base according to the difference between the pair of commodities. For example, in the case of USD to JPY and AUD to JPY, it would show something like 100 JPY = 0.66 USD and 100 JPY = 0.91 AUD, which is much more useful than above.

Disclosure: the implementation is mostly done by AI, but I reviewed the code and think it is a reasonable implementation.


def price_base(value: Decimal) -> Decimal:
"""Return a scaled base amount for displaying a price."""
exponent = max(0, round(-value.log10())) if value > ZERO else 0

@upsuper upsuper Jun 19, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using round here means that the result would be consistent if the number is floating around power of 10, e.g. we can have both 1 EUR = 1.18 USD and 1 EUR = 0.97 USD, rather than 10 EUR = 9.70 USD. The latter is what it would be if it's floor here. But in fact it just pushes the boundary towards $\sqrt{10} \approx 3.16$, so if the value floats around that, the base can still switch back and forth.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant