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

Proposal: Add function NewFromFloat #108

Closed
npinochet opened this issue May 5, 2022 · 6 comments
Closed

Proposal: Add function NewFromFloat #108

npinochet opened this issue May 5, 2022 · 6 comments

Comments

@npinochet
Copy link
Contributor

npinochet commented May 5, 2022

It would be really useful to be able to create a new Money struct from a float or maybe a decimal string ('1.243'). Formatting the decimal amount from a source (API response, file, ...) to a Money struct can get really tedious, that's why I propose a util function inside the money package for dealing with this cases.

This is my current implementation:

func NewFromFloat(amount float64, currency string) *money.Money {
	currencyDecimals := math.Pow(10, float64(money.GetCurrency(currency).Fraction))
	amountCents := int64(amount * currencyDecimals)
	return money.New(amountCents, currency)
}

To not lose precision dealing with floats there can also be a NewFromString, but I haven gotten around to implementing it.

What do you guys think?

@Rhymond
Copy link
Owner

Rhymond commented Jul 21, 2022

Hi @npinochet, great idea! I would love to see it implemented, I cannot find any issues with your code example

npinochet added a commit to npinochet/go-money that referenced this issue Jul 21, 2022
@npinochet
Copy link
Contributor Author

Perfect, I went ahead and made the PR #115

npinochet added a commit to npinochet/go-money that referenced this issue Jul 25, 2022
npinochet added a commit to npinochet/go-money that referenced this issue Jul 25, 2022
@azlancpool
Copy link

Hi @Rhymond, first of all thank you so much for this great package!
When will we have a new release with these changes?

@Rhymond
Copy link
Owner

Rhymond commented Aug 1, 2022

Hey @azlancpool, Thanks! I just reviewed a PR and left couple of comments, if you're interested in NewFromFloat that's probably going to be merged soon.

npinochet added a commit to npinochet/go-money that referenced this issue Aug 2, 2022
npinochet added a commit to npinochet/go-money that referenced this issue Aug 2, 2022
npinochet added a commit to npinochet/go-money that referenced this issue Aug 2, 2022
npinochet added a commit to npinochet/go-money that referenced this issue Aug 2, 2022
Rhymond pushed a commit that referenced this issue Aug 3, 2022
@Rhymond
Copy link
Owner

Rhymond commented Aug 3, 2022

@azlancpool NewFromFloat merged and released in v1.0.9

@npinochet
Copy link
Contributor Author

As this issue is only for NewFromFloat, I'll close the issue since it's done 👍

npinochet added a commit to npinochet/go-money that referenced this issue Aug 3, 2022
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

3 participants