bojanz / currency

Currency handling for Go.

Home Page:https://pkg.go.dev/github.com/bojanz/currency

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Number() gives exponent formatting sometimes

plarsson opened this issue · comments

When using the RoundTo method with a precision of 8 and RoundHalfUp rounding mode on a zero amount, the Number method returns "0E-8" instead of "0.00000000". This inconsistency in formatting can cause confusion. Here's a snippet to reproduce the issue:

package main

import (
	"fmt"
	"github.com/bojanz/currency"
)

func main() {
	c, _ := currency.NewAmountFromInt64(0, "SEK")
	c = c.RoundTo(8, currency.RoundHalfUp)
	fmt.Printf("%s\n", c.Number())
}

The apd.Decimal String() defaults to use d.Text('G') which is intended?

// 'G' like 'E' for large exponents, like 'f' otherwise

One fix would be to change Number() to use apd.Decimal.Text('f') method but that will be a breaking change

It would be nice to be able to output Number() without exponents.

I would argue that by design we never want to display exponents.

I think this got fixed in cockraochdb/apd 3.2.0: cockroachdb/apd#128

I updated our version yesterday: a0d9214 but haven't tagged a release yet. I'll do so now.

Released v1.1.3, let me know if you encounter any further issues.

Works fine. Thanks for quick response!