Documentation ¶
Overview ¶
Package currency contains ISO 4217 currency codes and currency names.
Each currency contain it's name and decimal numbers. Since each currency have different decimal numbers, the database and internal application logic processes currency amounts as integers in smallest currency unit.
The package allows to process currency conversions from external (float) format into internal (integer) and vice versa.
For more information about ISO 4217 currency codes see https://www.iso.org/iso-4217-currency-codes.html
Example ¶
package main import ( "fmt" "github.com/ilyakaznacheev/tiny-wallet/pkg/currency" ) func main() { strUSD := "USD" strCLP := "CLP" strISK := "ISK" // convert string codes into Currency codes usd, _ := currency.AtoCurrency(strUSD) clp, _ := currency.AtoCurrency(strCLP) isk, _ := currency.AtoCurrency(strISK) // print currencies // each currency will print its name fmt.Println(usd) fmt.Println(clp) fmt.Println(isk) }
Output: US Dollar Chilean Peso Iceland Krona
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConvertToExternal ¶
ConvertToExternal converts internal integer amount in the lowest unit of the currency to external floating point format
E.g. USD (2): 1525 -> 15.25
Example ¶
package main import ( "fmt" "github.com/ilyakaznacheev/tiny-wallet/pkg/currency" ) func main() { amountIntUSD := 123450 amountIntIQD := 145345 amountIntISK := 25 // convert external (normal) money representations // into internal (integer) format fmt.Println(currency.ConvertToExternal(amountIntUSD, currency.USD)) fmt.Println(currency.ConvertToExternal(amountIntIQD, currency.IQD)) fmt.Println(currency.ConvertToExternal(amountIntISK, currency.ISK)) }
Output: 1234.5 145.345 25
func ConvertToInternal ¶
ConvertToInternal converts external floating point currency amount to internal integer in the lowest unit of the currency E.g. USD (2): 15.25 -> 1525
Example ¶
package main import ( "fmt" "github.com/ilyakaznacheev/tiny-wallet/pkg/currency" ) func main() { amountExtUSD := 1234.50 amountExtIQD := 145.345 amountExtISK := 25.0 // convert external (normal) money representations // into internal (integer) format fmt.Println(currency.ConvertToInternal(amountExtUSD, currency.USD)) fmt.Println(currency.ConvertToInternal(amountExtIQD, currency.IQD)) fmt.Println(currency.ConvertToInternal(amountExtISK, currency.ISK)) }
Output: 123450 145345 25
Types ¶
type Currency ¶
type Currency string
Currency type for a currency ISO 4217 code
const ( AFN Currency = "AFN" AED Currency = "AED" ALL Currency = "ALL" AMD Currency = "AMD" ANG Currency = "ANG" AOA Currency = "AOA" ARS Currency = "ARS" AUD Currency = "AUD" AWG Currency = "AWG" AZN Currency = "AZN" BAM Currency = "BAM" BBD Currency = "BBD" BDT Currency = "BDT" BGN Currency = "BGN" BHD Currency = "BHD" BIF Currency = "BIF" BMD Currency = "BMD" BND Currency = "BND" BOB Currency = "BOB" BOV Currency = "BOV" BRL Currency = "BRL" BSD Currency = "BSD" BTN Currency = "BTN" BWP Currency = "BWP" BYN Currency = "BYN" BZD Currency = "BZD" CAD Currency = "CAD" CDF Currency = "CDF" CHE Currency = "CHE" CHF Currency = "CHF" CHW Currency = "CHW" CLF Currency = "CLF" CLP Currency = "CLP" CNY Currency = "CNY" COP Currency = "COP" COU Currency = "COU" CRC Currency = "CRC" CUC Currency = "CUC" CUP Currency = "CUP" CVE Currency = "CVE" CZK Currency = "CZK" DJF Currency = "DJF" DKK Currency = "DKK" DOP Currency = "DOP" DZD Currency = "DZD" EGP Currency = "EGP" ERN Currency = "ERN" ETB Currency = "ETB" EUR Currency = "EUR" FJD Currency = "FJD" FKP Currency = "FKP" GBP Currency = "GBP" GEL Currency = "GEL" GHS Currency = "GHS" GIP Currency = "GIP" GMD Currency = "GMD" GNF Currency = "GNF" GTQ Currency = "GTQ" GYD Currency = "GYD" HKD Currency = "HKD" HNL Currency = "HNL" HRK Currency = "HRK" HTG Currency = "HTG" HUF Currency = "HUF" IDR Currency = "IDR" ILS Currency = "ILS" INR Currency = "INR" IQD Currency = "IQD" IRR Currency = "IRR" ISK Currency = "ISK" JMD Currency = "JMD" JOD Currency = "JOD" JPY Currency = "JPY" KES Currency = "KES" KGS Currency = "KGS" KHR Currency = "KHR" KMF Currency = "KMF" KPW Currency = "KPW" KRW Currency = "KRW" KWD Currency = "KWD" KYD Currency = "KYD" KZT Currency = "KZT" LAK Currency = "LAK" LBP Currency = "LBP" LKR Currency = "LKR" LRD Currency = "LRD" LSL Currency = "LSL" LYD Currency = "LYD" MAD Currency = "MAD" MDL Currency = "MDL" MGA Currency = "MGA" MKD Currency = "MKD" MMK Currency = "MMK" MNT Currency = "MNT" MOP Currency = "MOP" MRU Currency = "MRU" MUR Currency = "MUR" MVR Currency = "MVR" MWK Currency = "MWK" MXN Currency = "MXN" MXV Currency = "MXV" MYR Currency = "MYR" MZN Currency = "MZN" NAD Currency = "NAD" NGN Currency = "NGN" NIO Currency = "NIO" NOK Currency = "NOK" NPR Currency = "NPR" NZD Currency = "NZD" OMR Currency = "OMR" PAB Currency = "PAB" PEN Currency = "PEN" PGK Currency = "PGK" PHP Currency = "PHP" PKR Currency = "PKR" PLN Currency = "PLN" PYG Currency = "PYG" QAR Currency = "QAR" RON Currency = "RON" RSD Currency = "RSD" RUB Currency = "RUB" RWF Currency = "RWF" SAR Currency = "SAR" SBD Currency = "SBD" SCR Currency = "SCR" SDG Currency = "SDG" SEK Currency = "SEK" SGD Currency = "SGD" SHP Currency = "SHP" SLL Currency = "SLL" SOS Currency = "SOS" SRD Currency = "SRD" SSP Currency = "SSP" STN Currency = "STN" SVC Currency = "SVC" SYP Currency = "SYP" SZL Currency = "SZL" THB Currency = "THB" TJS Currency = "TJS" TMT Currency = "TMT" TND Currency = "TND" TOP Currency = "TOP" TRY Currency = "TRY" TTD Currency = "TTD" TWD Currency = "TWD" TZS Currency = "TZS" UAH Currency = "UAH" UGX Currency = "UGX" USD Currency = "USD" USN Currency = "USN" UYI Currency = "UYI" UYU Currency = "UYU" UYW Currency = "UYW" UZS Currency = "UZS" VES Currency = "VES" VND Currency = "VND" VUV Currency = "VUV" WST Currency = "WST" XAF Currency = "XAF" XCD Currency = "XCD" XDR Currency = "XDR" XOF Currency = "XOF" XPF Currency = "XPF" XSU Currency = "XSU" XUA Currency = "XUA" YER Currency = "YER" ZAR Currency = "ZAR" ZMW Currency = "ZMW" ZWL Currency = "ZWL" )
func AtoCurrency ¶
AtoCurrency converts string to ISO 4216 currency.
If there is no such currency code, the method will return an error
Example ¶
package main import ( "fmt" "github.com/ilyakaznacheev/tiny-wallet/pkg/currency" ) func main() { strUSD := "USD" strCLP := "CLP" strISK := "ISK" mustConvert := func(a string) currency.Currency { c, _ := currency.AtoCurrency(a) return *c } usd := mustConvert(strUSD) clp := mustConvert(strCLP) isk := mustConvert(strISK) fmt.Printf("%T: %#v: %s\n", usd, usd, usd) fmt.Printf("%T: %#v: %s\n", clp, clp, clp) fmt.Printf("%T: %#v: %s\n", isk, isk, isk) }
Output: currency.Currency: "USD": US Dollar currency.Currency: "CLP": Chilean Peso currency.Currency: "ISK": Iceland Krona
func (Currency) FormatAmount ¶
FormatAmount returns an integer amount formatted depending on the number of decimal places of the currency