Giter Site home page Giter Site logo

fpmoney's Issues

Serialize amount as string

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.

Support percent values

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.