money

package module
v0.0.0-...-2119dfd Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2021 License: MIT Imports: 8 Imported by: 0

README

github-status-action status

Go Money

Golang pure money calculation library provides safety and ease of use.

Usage

go-money can parse money string to money type but you can use numbers as input too.


	moneyOne, _ := money.ParseCAD("-$100.90")
	moneyTwo, _ := money.ParseCAD(".90$")

	result := moneyOne.Sub(*moneyTwo)
	fmt.Printf("money is : %v\n", result) // will prints money is : CAD$-101.80

You can pass all valid money string like $CAD-9.0 , CAD$79 , 23.65 , -0.9 , .09 , 78.98CAD , 98.07$ and ...

It also provides inner umarshaler,unmarshaler,Scanner and valuer which make it super easy to use

unmarshal
	type jsonType struct {
		User  string    `json:"user"`
		Money money.CAD `json:"money"`
	}
	var someStruct jsonType
	someJsonString := `{"user":"amupxm","money":"-78.23"}`
	json.Unmarshal([]byte(someJsonString), &someStruct)
	fmt.Printf("Unmarshaled money as cents : %v\n", someStruct.Money.AsCent()) // Unmarshaled money as cents : -7823


marshal
	data := struct {
		Type  string    `json:"type"`
		Money money.CAD `json:"money"`
	}{
		Type:  "Offer",
		Money: *money.NewMoney(99, 99),
	}
	b, _ := json.Marshal(data)
	fmt.Printf("Marshaled money is : %v\n", string(b)) // Marshaled money is : {"type":"Offer","money":"CAD$99.99"}

And you can scan,value to sql/database without any troubles =)

    var theMoney money.CAD
	data := struct {
		UserName  string    `json:"user_name"`
		Money money.CAD `json:"money"`
	}{
		UserName:  "amupxm",
		Money: *money.NewMoney(99, 99),
	}
	err := pg.db.QueryRow(`INSERT INTO transactions (username, money)
	 VALUES ($1, $2) RETURNING money`, user.UserName,user.money).Scan(&theMoney)
    fmt.Println("Money is %v\n" ,theMoney )  // Money is : CAD$99.99

Money type in compatible with postgres monet type.

Give star if interested 🌟

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CAD

type CAD struct {
	// contains filtered or unexported fields
}

CAD is canadian dollar

func Cents

func Cents(i int64) CAD

Cents is a helper function to create a CAD from cents

func NewMoney

func NewMoney(dollars, cents int64) *CAD

NewMoney creates a new CAD

func ParseCAD

func ParseCAD(s string) (*CAD, error)

ParseCAD parses a string into a CAD

func (CAD) Abs

func (c CAD) Abs() CAD

Abs returns the absolute value of the amount.

func (CAD) Add

func (c CAD) Add(o CAD) CAD

Add adds the given amount to the current amount.

func (CAD) AsCent

func (c CAD) AsCent() int64

AsCent returns the amount in cents

func (CAD) CanonicalForm

func (c CAD) CanonicalForm() (dollar int64, cent int64)

CanonicalForm returns the canonical form of the amount.

func (CAD) GoString

func (c CAD) GoString() string

GoString creates sting

func (CAD) MarshalJSON

func (c CAD) MarshalJSON() ([]byte, error)

MarshalJSON marshals the money to JSON.

func (CAD) Mul

func (c CAD) Mul(scalar int64) CAD

Mul multiplies the amount by the given factor.

func (*CAD) Scan

func (c *CAD) Scan(src interface{}) (err error)

Scan implements the sql.Scanner interface.

func (CAD) String

func (c CAD) String() string

func (CAD) Sub

func (c CAD) Sub(o CAD) CAD

Sub subtracts the given amount from the current amount.

func (*CAD) UnmarshalJSON

func (c *CAD) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the money from JSON.

func (CAD) Value

func (c CAD) Value() (driver.Value, error)

Value implements the driver Valuer interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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