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

Use number of minor units in one major instead of a fraction #88

Open
dzyanis opened this issue Jun 3, 2021 · 0 comments
Open

Use number of minor units in one major instead of a fraction #88

dzyanis opened this issue Jun 3, 2021 · 0 comments

Comments

@dzyanis
Copy link

dzyanis commented Jun 3, 2021

I know that most majority of currencies have 100 minor units in one major or don't have a minor at all and it's okay to save their fraction (2, 0 or 3) and use them for converting:

func (f *Formatter) ToMajorUnits(amount int64) float64 {
	if f.Fraction == 0 {
		return float64(amount)
	}

	return float64(amount) / float64(math.Pow10(f.Fraction))
}

But my suggestion is to be more pragmatic and remember about non-decimal currency and etc:

  • Malagasy ariary (1 ariary = 5 iraimbilanja), Mauritanian ouguiya and etc.;
  • Old currencies: 1 Thaler = 36 Mariengroschen = 288 Pfennige;
  • Fictional currencies: 1 Galleon = 17 Sickles = 493 Knuts;
  • Cryptocurrencies: 1 BTC = 100'000'000 Satoshi

And if we use the number of minor units we can remove this math.Pow10 function and the result will look like:

func (f *Formatter) ToMajorUnits(amount int64) float64 {
	if f.Units == 0 {
		return float64(amount)
	}

	return float64(amount) / float64(f.Units)
}
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

No branches or pull requests

1 participant