Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lang.FormatCurrency & lang.FormatAccounting do not allow PRECISION < 2 #8858

Closed
divinerites opened this issue Aug 5, 2021 · 6 comments · Fixed by #8950
Closed

lang.FormatCurrency & lang.FormatAccounting do not allow PRECISION < 2 #8858

divinerites opened this issue Aug 5, 2021 · 6 comments · Fixed by #8950
Labels

Comments

@divinerites
Copy link

divinerites commented Aug 5, 2021

What version of Hugo are you using (hugo version)?

$ hugo version
hugo v0.87.0+extended darwin/amd64 BuildDate=unknown

Does this issue reproduce with the latest release?

yes.

Not sure if it is related to the "Add new localized versions of lang.FormatNumber etc." from v0.87.0, or if it is intended (not on doc) but I can't get PRECISION = 0 or 1 for lang.FormatCurrency & lang.FormatAccounting

{{ 100 | lang.FormatCurrency 0 "CHF" }}<br>
{{ 100 | lang.FormatCurrency 1 "CHF" }}<br>
{{ 100 | lang.FormatCurrency 2 "CHF" }}<br>
{{ 100 | lang.FormatCurrency 3 "CHF" }}<br>
{{ 100 | lang.FormatAccounting 0 "CHF" }}<br>
{{ 100 | lang.FormatAccounting 1 "CHF" }}<br>
{{ 100 | lang.FormatAccounting 2 "CHF" }}<br>
{{ 100 | lang.FormatAccounting 3 "CHF" }}<br>
{{ 100 | lang.FormatNumber 0 }}<br>
{{ 100 | lang.FormatNumber 1 }}<br>
{{ 100 | lang.FormatNumber 2 }}<br>
{{ 100 | lang.FormatNumber 3 }}<br>

produce

100,00 CHF
100,00 CHF
100,00 CHF
100,000 CHF
100,00 CHF
100,00 CHF
100,00 CHF
100,000 CHF
100
100,0
100,00
100,000
ptgott added a commit to ptgott/hugo that referenced this issue Aug 6, 2021
@ptgott
Copy link
Contributor

ptgott commented Aug 6, 2021

I'm attempting to reproduce the issue via unit tests in my feature branch (see the diff here). I've reproduced the issue with *Namespace.FormatCurrency() and *Namespace.FormatAccounting() when the precision is zero, though not one:

Running tool: /usr/local/bin/go test -timeout 30s -run ^TestFormatNumbers$ github.com/gohugoio/hugo/tpl/lang

--- FAIL: TestFormatNumbers (0.00s)
    --- FAIL: TestFormatNumbers/FormatAccounting (0.00s)
        /Users/paulgottschling/OneDrive/Programming/active-repos/hugo/tpl/lang/lang_test.go:118: 
            error:
              values are not equal
            got:
              "100,00 CHF"
            want:
              "100 CHF"
            stack:
              /Users/paulgottschling/OneDrive/Programming/active-repos/hugo/tpl/lang/lang_test.go:118
                c.Assert(gotFr, qt.Equals, "100 CHF")
            
    --- FAIL: TestFormatNumbers/FormatCurrency (0.00s)
        /Users/paulgottschling/OneDrive/Programming/active-repos/hugo/tpl/lang/lang_test.go:103: 
            error:
              values are not equal
            got:
              "100,00 CHF"
            want:
              "100 CHF"
            stack:
              /Users/paulgottschling/OneDrive/Programming/active-repos/hugo/tpl/lang/lang_test.go:103
                c.Assert(gotFr, qt.Equals, "100 CHF")
            
FAIL
FAIL	github.com/gohugoio/hugo/tpl/lang	0.024s
FAIL

I'm working on reproducing this via Hugo template (see my unit test in language_test.go), though I'm still getting acquainted with the repo and haven't managed to get the site to build in the test.

@ptgott
Copy link
Contributor

ptgott commented Aug 7, 2021

Reproduced this in a unit test that builds the site with the template provided in the issue description (https://github.com/ptgott/hugo/blob/97d12f274272b3fb6824a14cef2f8e0449b05569/hugolib/language_test.go#L141):

Running tool: /usr/local/bin/go test -timeout 30s -run ^TestLanguageCurrencyAccountingZeroAndOne$ github.com/gohugoio/hugo/hugolib

--- FAIL: TestLanguageCurrencyAccountingZeroAndOne (0.01s)
    /Users/paulgottschling/OneDrive/Programming/active-repos/hugo/hugolib/language_test.go:173: No match for "FormatCurrency with precision 0: 100 CHF<br>" in content for public/fr_ch/index.html
        
        
        FormatCurrency with precision 0: 100,00 CHF<br>
        FormatCurrency with precision 1: 100,00 CHF<br>
        FormatCurrency with precision 2: 100,00 CHF<br>
        FormatCurrency with precision 3: 100,000 CHF<br>
        FormatAccounting with precision 0: 100,00 CHF<br>
        FormatAccounting with precision 1: 100,00 CHF<br>
        FormatAccounting with precision 2: 100,00 CHF<br>
        FormatAccounting with precision 3: 100,000 CHF<br>	
        
        "\n\nFormatCurrency with precision 0: 100,00 CHF<br>\nFormatCurrency with precision 1: 100,00 CHF<br>\nFormatCurrency with precision 2: 100,00 CHF<br>\nFormatCurrency with precision 3: 100,000 CHF<br>\nFormatAccounting with precision 0: 100,00 CHF<br>\nFormatAccounting with precision 1: 100,00 CHF<br>\nFormatAccounting with precision 2: 100,00 CHF<br>\nFormatAccounting with precision 3: 100,000 CHF<br>\t\n"
FAIL
FAIL	github.com/gohugoio/hugo/hugolib	0.034s
FAIL

@ptgott
Copy link
Contributor

ptgott commented Aug 7, 2021

@divinerites It looks like FormatAccounting and FormatCurrency call the FmtAccounting and FmtCurrency methods of a locales.Translator (here), and always leaving two figures after the decimal is the way these functions are supposed to work (e.g., see here).

@bep should this behavior be documented?

@jmooring
Copy link
Member

jmooring commented Aug 8, 2021

Most currencies have two minor units, but per ISO 4217 there are 17 currencies with zero minor units: BIF, CLP, DJF, GNF, ISK, JPY, KMF, KRW, PYG, RWF, UGX, UYI, VND, VUV, XAF, XOF, XPF.

The go-playground/locales module, from which gohugoio/locales was forked, has no provision for minor unit determination. The minimum number of minor units is always two. For example:

https://github.com/go-playground/locales/blob/master/en/en.go#L334-L343

There are two reasons for displaying zero minor units:

  1. Stylistic. For example, I want to display $100 instead of $100.00, even though $100.00 is technically correct.
  2. Technical. It doesn't make sense to display ¥100.00 because the Japanese yen has no minor units; it should always be displayed as an integer.

I cannot think of a valid reason to display one minor unit, but there could be.

@divinerites
Copy link
Author

divinerites commented Aug 8, 2021

  1. Stylistic. For example, I want to display $100 instead of $100.00,

This is exactly my use case.
The minor units are useless for our customers and we want our prices to be short.

But I can understand why it is technically like this now, and I'll keep my old code for those use cases.
Using this function only for prices without digits. No problem.

ptgott added a commit to ptgott/hugo that referenced this issue Sep 2, 2021
The documentation of the FormatAccounting and FormatCurrency
functions could be clearer in terms of how the precision param
works. This commit makes it more explicit that adding a precision
of < 2 will not format the return values to include fewer decimals.

Resolves gohugoio#8858
ptgott added a commit to ptgott/hugo that referenced this issue Sep 2, 2021
The documentation of the FormatAccounting and FormatCurrency
functions could be clearer in terms of how the precision param
works. This commit makes it more explicit that adding a precision
of < 2 will not format the return values to include fewer decimals.

Resolves gohugoio#8858
ptgott added a commit to ptgott/hugo that referenced this issue Sep 11, 2021
The documentation of the FormatAccounting and FormatCurrency
functions could be clearer in terms of how the precision param
works. This commit makes it more explicit that adding a precision
of < 2 will not format the return values to include fewer decimals.

Resolves gohugoio#8858
ptgott added a commit to ptgott/hugo that referenced this issue Sep 19, 2021
The documentation of the FormatAccounting and FormatCurrency
functions could be clearer in terms of how the precision param
works. This commit makes it more explicit that adding a precision
of < 2 will not format the return values to include fewer decimals.

Resolves gohugoio#8858
@bep bep closed this as completed in #8950 Sep 22, 2021
bep pushed a commit that referenced this issue Sep 22, 2021
The documentation of the FormatAccounting and FormatCurrency
functions could be clearer in terms of how the precision param
works. This commit makes it more explicit that adding a precision
of < 2 will not format the return values to include fewer decimals.

Resolves #8858
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants