Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A lightweight solution for handling and storing money.

License

NotificationsYou must be signed in to change notification settings

theocodes/monetized

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

93 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build StatusDeps StatusInline docs

As a general rule, using floats to store money is abad idea.

Enter Monetized. A library that aims to facilitate the handling of money through a safe way to store currency values andand providing abstractions to perform arithmetical operations on those values.

Monetized leveragesDecimal's ability to safely handle arbitrary precision to perform calculationson money values.

A typical%Monetized.Money{} struct will contain a value in the shape of aDecimal struct and a currency (if any) as a string.

Requires elixir version 1.3.x or above as per ecto 2.x

Usage

Monetized also ships with aEcto.Type that gives us an easier way to store money.

aliasMonetized.Moneyschema"transaction"dofield:description,:stringfield:balance,Money,default:Money.zerotimestampsend

By doing this we're able to pass a%Money{} struct to the changeset andinsert in theRepo since Ecto knows how to convert that struct into a primitivetype to save and back when you read from it.

balance=Money.make("£ 100.50")changeset=Transaction.changeset(%Transaction{},%{description:"Invoice payment",balance:balance})# Orbalance=%{value:"100.50",currency:"GBP"}changeset=Transaction.changeset(%Transaction{},%{description:"Invoice payment",balance:balance})

Check thedocs for more.

Other usage

aliasMonetized.MoneyaliasMonetized.Math# Create with a stringiex>item_one=Money.make("£ 200.50")#Money<200.50GBP># Or a floatiex>item_two=Money.make(10.25,[currency:"GBP"])#Money<10.25GBP># Adding two moneys togetheriex>Math.add(item_one,item_two)#Money<210.75GBP># Or an integeriex>balance=Money.make(100_000,[currency:"USD"])#Money<100000.00USD># Substracting from money (currency inferred from balance)iex>result=Math.sub(balance,50_000)#Money<50000.00USD># Getting the string representationiex>Money.to_string(result,[currency_symbol:true])"$ 50,000.00"# You can also use `from_integer/2`, `from_float/2`, `from_decimal/2` and `from_string/2`# respectively if the desired behavior is to raise when the amount is# not of the expected type.# If either the symbol or the currency key is found within the string,# that currency will be used. However, if a different currency is given in the# options, it will take precedence.iex>Money.from_string("£ 200")#Money<200.00GBP>iex>Money.from_string("200 EUR")#Money<200.00EUR>iex>Money.from_integer(200)#Money<200.00>iex>Money.from_float(200.50)#Money<200.50>iex>decimal=Decimal.new(10.25)...>Money.from_decimal(decimal,currency:"EUR")#Money<10.15EUR>iex>Money.from_integer("10")**(FunctionClauseError) no function clause matchinginMonetized.Money.from_integer/2(monetized) lib/money.ex:204: Monetized.Money.from_integer("10",[])

Check thedocs for more examples

Config

config:monetized,config:[delimiter:",",separator:".",currency:"USD",format:"%cs %n%s%d"]

Installation

Add monetized to your list of dependencies inmix.exs:

defdepsdo[{:monetized,"~> 0.5.0"}]end

TODO

  • New currencies can be added through the package's config
  • 1.0.0 ! yay!

Benchmarking

$mix deps.get$MIX_ENV=bench mix compile$MIX_ENV=bench mix bench

Contributing

SeeCONTRIBUTING.md for details.

Licensing

SeeLICENSE.md

About

A lightweight solution for handling and storing money.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors12

Languages


[8]ページ先頭

©2009-2025 Movatter.jp