nikolaydubina / fpmoney Goto Github PK
View Code? Open in Web Editor NEW🧧 Fixed-Point Decimal Money
License: MIT License
🧧 Fixed-Point Decimal Money
License: MIT License
The fpmoney.Amount
serializes values to JSON as a floating point amount, and a currency code like this:
{
"amount": 128.45,
"currency": "ARS"
}
That is used in frontends and persisted in databases as a floating point number.
Other currency packages serialize the amount as a string instead, to avoid conversions and lost of precision. For example:
{
"amount": "128.45",
"currency": "ARS"
}
What do you think of having a global option or be customized by the format string stored under the "json" key (or a money key) in the struct field's tag:
type Invoice struct {
TotalAmount fpmoney.Amount `json:"totalAmount,asString"`
}
I can provide a PR.
Money is usually used in context when a percent is computed on some money value, for example to compute an interest amount.
A Percent type could be added as a companion abstraction for money.
// Percent is a 3 decimal percent value. Internally it is stored as an int64 with a 3 digits scale
// interestRate := percent.FromStr("3.5")
// It represents a 3.5% (a 0.03500) factor, and stored as 3500
type Percent int64
const Scale = 1000
const ScaledPercentToRate = 100 * Scale
func (p Percent) By(amount fpmoney.Amount) (computed fpmoney.Amount, remainder fpmoney.Amount) {
return amount.Mul(int(p)).Div(ScaledPercentToRate)
}
Additional methods will be provided.
What do you think?
I can provide a PR.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.