currency

package
v0.0.0-...-2824937 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2020 License: MIT, BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package currency contains currency-related functionality.

NOTE: the formatting functionality is currently under development and may change without notice.

Index

Examples

Constants

View Source
const CLDRVersion = "31"

CLDRVersion is the CLDR version from which the tables in this package are derived.

Variables

This section is empty.

Functions

This section is empty.

Types

type Amount

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

Amount is an amount-currency unit pair.

func (Amount) Currency

func (a Amount) Currency() Unit

Currency reports the currency unit of this amount.

func (Amount) Format

func (a Amount) Format(s fmt.State, verb rune)

Format implements fmt.Formatter. It accepts format.State for language-specific rendering.

type Formatter

type Formatter func(amount interface{}) formattedValue

Formatter decorates a given number, Unit or Amount with formatting options.

var (
	// Uses Narrow symbols. Overrides Symbol, if present.
	NarrowSymbol Formatter = Formatter(formNarrow)

	// Use Symbols instead of ISO codes, when available.
	Symbol Formatter = Formatter(formSymbol)

	// Use ISO code as symbol.
	ISO Formatter = Formatter(formISO)
)

func (Formatter) Default

func (f Formatter) Default(currency Unit) Formatter

Default creates a new Formatter that defaults to currency unit c if a numeric value is passed that is not associated with a currency.

func (Formatter) Kind

func (f Formatter) Kind(k Kind) Formatter

Kind sets the kind of the underlying currency unit.

type Kind

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

Kind determines the rounding and rendering properties of a currency value.

var (
	// Standard defines standard rounding and formatting for currencies.
	Standard Kind = Kind{/* contains filtered or unexported fields */}

	// Cash defines rounding and formatting standards for cash transactions.
	Cash Kind = Kind{/* contains filtered or unexported fields */}

	// Accounting defines rounding and formatting standards for accounting.
	Accounting Kind = Kind{/* contains filtered or unexported fields */}
)

func (Kind) Rounding

func (k Kind) Rounding(cur Unit) (scale, increment int)

Rounding reports the rounding characteristics for the given currency, where scale is the number of fractional decimals and increment is the number of units in terms of 10^(-scale) to which to round to.

type QueryIter

type QueryIter interface {
	// Next returns true if there is a next element available.
	// It must be called before any of the other methods are called.
	Next() bool

	// Unit returns the unit of the current iteration.
	Unit() Unit

	// Region returns the Region for the current iteration.
	Region() language.Region

	// From returns the date from which the unit was used in the region.
	// It returns false if this date is unknown.
	From() (time.Time, bool)

	// To returns the date up till which the unit was used in the region.
	// It returns false if this date is unknown or if the unit is still in use.
	To() (time.Time, bool)

	// IsTender reports whether the unit is a legal tender in the region during
	// the specified date range.
	IsTender() bool
}

QueryIter represents a set of Units. The default set includes all Units that are currently in use as legal tender in any Region.

func Query

func Query(options ...QueryOption) QueryIter

Query represents a set of Units. The default set includes all Units that are currently in use as legal tender in any Region.

Example
package main

import (
	"fmt"
	"time"

	"golang.org/x/text/currency"
)

func main() {
	t1799, _ := time.Parse("2006-01-02", "1799-01-01")
	for it := currency.Query(currency.Date(t1799)); it.Next(); {
		from := ""
		if t, ok := it.From(); ok {
			from = t.Format("2006-01-01")
		}
		fmt.Printf("%v is used in %v since: %v\n", it.Unit(), it.Region(), from)
	}
}
Output:

GBP is used in GB since: 1694-07-07
GIP is used in GI since: 1713-01-01
USD is used in US since: 1792-01-01

type QueryOption

type QueryOption func(*iter)

A QueryOption can be used to change the set of unit information returned by a query.

var Historical QueryOption = historical

Historical selects the units for all dates.

var NonTender QueryOption = nonTender

NonTender returns a new query that also includes matching Units that are not legal tender.

func Date

func Date(t time.Time) QueryOption

Date queries the units that were in use at the given point in history.

func Region

func Region(r language.Region) QueryOption

Region limits the query to only return entries for the given region.

type Unit

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

Unit is an ISO 4217 currency designator.

var (
	// Undefined and testing.
	XXX Unit = Unit{}
	XTS Unit = Unit{xts}

	// G10 currencies https://en.wikipedia.org/wiki/G10_currencies.
	USD Unit = Unit{usd}
	EUR Unit = Unit{eur}
	JPY Unit = Unit{jpy}
	GBP Unit = Unit{gbp}
	CHF Unit = Unit{chf}
	AUD Unit = Unit{aud}
	NZD Unit = Unit{nzd}
	CAD Unit = Unit{cad}
	SEK Unit = Unit{sek}
	NOK Unit = Unit{nok}

	// Additional common currencies as defined by CLDR.
	BRL Unit = Unit{brl}
	CNY Unit = Unit{cny}
	DKK Unit = Unit{dkk}
	INR Unit = Unit{inr}
	RUB Unit = Unit{rub}
	HKD Unit = Unit{hkd}
	IDR Unit = Unit{idr}
	KRW Unit = Unit{krw}
	MXN Unit = Unit{mxn}
	PLN Unit = Unit{pln}
	SAR Unit = Unit{sar}
	THB Unit = Unit{thb}
	TRY Unit = Unit{try}
	TWD Unit = Unit{twd}
	ZAR Unit = Unit{zar}

	// Precious metals.
	XAG Unit = Unit{xag}
	XAU Unit = Unit{xau}
	XPT Unit = Unit{xpt}
	XPD Unit = Unit{xpd}
)

func FromRegion

func FromRegion(r language.Region) (currency Unit, ok bool)

FromRegion reports the currency unit that is currently legal tender in the given region according to CLDR. It will return false if region currently does not have a legal tender.

func FromTag

func FromTag(t language.Tag) (Unit, language.Confidence)

FromTag reports the most likely currency for the given tag. It considers the currency defined in the -u extension and infers the region if necessary.

func MustParseISO

func MustParseISO(s string) Unit

MustParseISO is like ParseISO, but panics if the given currency unit cannot be parsed. It simplifies safe initialization of Unit values.

func ParseISO

func ParseISO(s string) (Unit, error)

ParseISO parses a 3-letter ISO 4217 currency code. It returns an error if s is not well-formed or not a recognized currency code.

func (Unit) Amount

func (u Unit) Amount(amount interface{}) Amount

Amount creates an Amount for the given currency unit and amount.

func (Unit) String

func (u Unit) String() string

String returns the ISO code of u.

Jump to

Keyboard shortcuts

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