dough

package module
v0.4.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 28, 2020 License: MIT Imports: 6 Imported by: 0

README

Dough

Dough

"Roll Out" your golang currency issues with Dough.

Build Status

Installation

$ go get -u github.com/fluidpay/dough

Currency Functions

Here are the available functions to help deal with currency issues you may run into.

StringToInt("$5", "USD") // output = 500

DisplayFull(10, "USD") // output = "$0.10"

DisplayWithAlpha(0, "USD") // output = "USD 0.00"

DisplayNoSymbol(10, "USD") // output = "0.10"

DisplayWithDecimal(10, "USD") // output = "0.10"

TopCurrencies() // output = []string{"USD", "EUR", "GBP", "INR", "CRC", "VND", "HUF", "ILS", "CNY", "KRW", "NGN", "PYG", "PHP", "PLN", "THB", "UAH", "JPY"}

ListCurrencies([]string{"USD"}) // output = []Currency{{Unit: "US Dollar", Alpha: "USD", Numeric: "840", Symbol: "\u0024", Fraction: 2, Decimal: ".", Grouping: 3, Delimiter: ",", SymbolPositionFront: true}}

Additional Functions

Here are additional functions that may come in handy to your currency needs.

GetISOFromAlpha("USD") // output = Currency{Unit: "US Dollar", Alpha: "USD", Numeric: "840", Symbol: "$", Fraction: 2, Decimal: ".", Grouping: 3, Delimiter: ",", SymbolPositionFront: true}

GetISOCodeFromNumeric("840") // output = "840"

GetAlphaFromISONumeric("840") // output = "USD"

ConvertToStringWithDecimal(100, 2) // output = "1.00"

InsertDelimiter("0001", 3, ",") // output = "000,1"

SwapSymbolWithAlpha("$0.00", "$", "USD") // output = "USD 0.00"

IsNegative(-1) // output = true

FormatCurrency(1, Currency{Unit: "US Dollar", Alpha: "USD", Numeric: "840", Symbol: "\u0024", Fraction: 2, Decimal: ".", Grouping: 3, Delimiter: ",", SymbolPositionFront: true}) // output = "$0.01"

FloatToInt(9.99, 2) // output = 999

IntToFloat(999, 2) // output = 9.99

PercentageFromInt(898, 56.7, 3, Round) // output = 509.166

PercentageFromFloat(11.11, 13, 4, Round) // output = 1.4443

MaskCard("4111111111111111") // output = "411111", "1111", "411111******1111"

MaskACHAccount("8114460248") // output = "81******48"

ValidLuhn("4111111111111111") // output = true

GetCardType("4111111111111111") // output = "visa"

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

Documentation

Index

Constants

View Source
const (
	Round   round = "round"
	Floor   round = "floor"
	Ceil    round = "ceil"
	Bankers round = "bankers"
)

Variables

View Source
var (
	ErrCardLength      = errors.New("card length should be > 10")
	ErrACHLength       = errors.New("account length should be > 4")
	ErrUnknownCardType = errors.New("unknown card type")
)

errors

View Source
var (
	AMEXCardFormatRegex     = regexp.MustCompile(amexCardFormatString)
	VISACardFormatRegex     = regexp.MustCompile(visaCardFormatString)
	MasterCardFormatRegex   = regexp.MustCompile(mastercardCardFormatString)
	DISCOVERCardFormatRegex = regexp.MustCompile(discoverCardFormatString)
	JCBCardFormatRegex      = regexp.MustCompile(jcbCardFormatString)
	DINERSCardFormatRegex   = regexp.MustCompile(dinersClubInternationalFormatString)
)

vars

View Source
var CurrencyList = map[string]Currency{}/* 157 elements not displayed */

CurrencyList - complete list of supported currencies

View Source
var ErrorInvalidISO = errors.New("Invalid ISO Code")

ErrorInvalidISO : returns an error for an invalid ISO code

View Source
var ErrorInvalidISOFractionMatch = errors.New("Invalid ISO Fraction Match")

ErrorInvalidISOFractionMatch : returns an error if fraction does not match ISO fraction

View Source
var ErrorInvalidStringFormat = errors.New("Invalid String Format")

ErrorInvalidStringFormat : returns an error if trying to convert an invalid string value

View Source
var ErrorUnableToFormatCurrency = errors.New("Unable To Format Currency")

ErrorUnableToFormatCurrency : returns an error for invalid currency formatting

View Source
var ErrorUnableToFormatCurrencyFromString = errors.New("Unable To Format Currency From String")

ErrorUnableToFormatCurrencyFromString : returns an error for invalid formatting from a string

Functions

func ConvertToStringWithDecimal

func ConvertToStringWithDecimal(num int, fraction int) string

ConvertToStringWithDecimal : returns the uint as a stringified float

func DisplayFull

func DisplayFull(num int, alpha string) (string, error)

DisplayFull : returns a string with full currency formatting... "num" being the amount, "alpha" being the ISO three digit alphabetic code.

func DisplayNoSymbol

func DisplayNoSymbol(num int, alpha string) (string, error)

DisplayNoSymbol : returns a string with full currency formatting minus the ISO symbol... "num" being the amount, "alpha" being the ISO three digit alphabetic code.

func DisplayWithAlpha

func DisplayWithAlpha(num int, alpha string) (string, error)

DisplayWithAlpha : returns a string with full currency formatting with the symbol replaced by the ISO three digit alphabetic code... "num" being the amount, "alpha" being the ISO three digit alphabetic code.

func DisplayWithDecimal

func DisplayWithDecimal(num int, alpha string) (string, error)

DisplayWithDecimal : returns a string with all currency formatting removed except decimal places... "num" being the amount, "alpha" being the ISO three digit alphabetic code.

func FloatToInt added in v0.1.8

func FloatToInt(amt float64, fraction int) int

FloatToInt will take in a float and based upon fraction will output the int version

func FormatCurrency

func FormatCurrency(num int, ISO Currency) string

FormatCurrency : returns basic currency formatting

func GetAlphaFromISONumeric

func GetAlphaFromISONumeric(num string) (string, error)

GetAlphaFromISONumeric : returns a formatted ISO alpha code from the ISO numeric counterpart

func GetCardType added in v0.2.0

func GetCardType(cardnum string) (string, error)

GetCardType Accepts a string containing a credit card number and validates it against some regexes to return the card type.

func GetISOCodeFromNumeric

func GetISOCodeFromNumeric(num string) (string, error)

GetISOCodeFromNumeric : returns a formatted ISO numeric code or an error if the ISO is not found

func InsertDelimiter

func InsertDelimiter(str string, group int, del string) string

InsertDelimiter : returns a new string with delimiter formatting

func IntToFloat added in v0.1.8

func IntToFloat(amt int, fraction int) float64

IntToFloat will take in a int and based upon fraction will output the float version

func IsNegative added in v0.1.6

func IsNegative(num int) bool

IsNegative : returns a bool based on whether the int is negative or positive

func MaskACHAccount added in v0.2.0

func MaskACHAccount(accountNumber string) (string, error)

MaskACHAccount takes in an account number and returns masked

func MaskCard added in v0.2.0

func MaskCard(cardnumber string) (string, string, string, error)

MaskCard takes in a card number and returns firstsix, lastfour, masked

func PercentageFromFloat added in v0.1.8

func PercentageFromFloat(amt float64, percentage float64, fraction int, round round) float64

PercentageFromFloat will give you a percentage to the exact precision that you want based on fraction

func PercentageFromInt added in v0.1.8

func PercentageFromInt(amt int, percentage float64, fraction int, round round) float64

PercentageFromInt will give you a percentage to the exact precision that you want based on fraction

func StringToInt added in v0.1.3

func StringToInt(num string, alpha string, options ...bool) (int, error)

StringToInt : returns a int from a string value

func SwapSymbolWithAlpha

func SwapSymbolWithAlpha(str string, sym string, alpha string) string

SwapSymbolWithAlpha : returns a string with the ISO alpha code instead of symbol

func ValidLuhn added in v0.2.0

func ValidLuhn(s string) bool

ValidLuhn returns a boolean indicating if the argument was valid according to the Luhn algorithm.

Types

type Currency

type Currency struct {
	Unit                string
	Alpha               string
	Numeric             string
	Symbol              string
	Fraction            int
	Decimal             string
	Grouping            int
	Delimiter           string
	SymbolPositionFront bool
}

Currency - struct containing currency variables

func GetISOFromAlpha

func GetISOFromAlpha(alpha string) (Currency, error)

GetISOFromAlpha : returns an ISO currency struct or an error if the ISO is not found

func GetISOFromNumeric added in v0.2.2

func GetISOFromNumeric(num string) (Currency, error)

GetISOFromNumeric : returns an ISO currency struct or an error if the ISO is not found

func ListCurrencies

func ListCurrencies(list []string) ([]Currency, error)

ListCurrencies : returns a list of currencies

func TopCurrencies

func TopCurrencies() ([]Currency, error)

TopCurrencies returns the list of top currencies based upon usage

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL