Documentation ¶
Overview ¶
Package decimal implements an arbitrary precision fixed-point decimal.
To use as part of a struct:
type Struct struct { Number Decimal }
The zero-value of a Decimal is 0, as you would expect.
The best way to create a new Decimal is to use decimal.NewFromString, ex:
n, err := decimal.NewFromString("-123.4567") n.String() // output: "-123.4567"
NOTE: This can "only" represent numbers with a maximum of 2^31 digits after the decimal point.
Package uuid provides implementation of Universally Unique Identifier (UUID). Supported versions are 1, 3, 4 and 5 (as specified in RFC 4122) and version 2 (as specified in DCE 1.1).
Index ¶
- Constants
- Variables
- func AesDecrypt(crypted, key []byte, iv []byte, unPaddingFunc func([]byte) []byte) ([]byte, error)
- func AesDecryptPkcs5(crypted []byte, key []byte, iv []byte) ([]byte, error)
- func AesDecryptPkcs7(crypted []byte, key []byte, iv []byte) ([]byte, error)
- func AesDecryptPkcs7Base64(crypted []byte, key []byte, iv []byte) ([]byte, error)
- func AesDecryptSimple(crypted []byte, key string, iv string) ([]byte, error)
- func AesEncrypt(origData []byte, key []byte, iv []byte, paddingFunc func([]byte, int) []byte) ([]byte, error)
- func AesEncryptPkcs5(origData []byte, key []byte, iv []byte) ([]byte, error)
- func AesEncryptPkcs7(origData []byte, key []byte, iv []byte) ([]byte, error)
- func AesEncryptPkcs7Base64(origData []byte, key []byte, iv []byte) ([]byte, error)
- func AesEncryptSimple(origData []byte, key string, iv string) ([]byte, error)
- func AttrToUnderscore(st interface{}) []string
- func CamelName(name string) string
- func CentToYuan(cent int64) float64
- func CheckErr(err error)
- func Equal(u1 UUID, u2 UUID) bool
- func GenerUUID() string
- func GetClientPublicIP(r *http.Request) string
- func GetCurve25519Key(private, public [32]byte) (Key [32]byte)
- func GetCurve25519KeypPair() (Aprivate, Apublic [32]byte)
- func GetExternalIP() (string, error)
- func GetIPAddress(ip string) (province string, city string, err error)
- func GetIntranetIP() (ips []string, err error)
- func GetRandomName() string
- func GetRandomSalt() string
- func GetRandomString(num int) string
- func GetSignStr(params map[string]interface{}) string
- func HMACSHA1(keyStr string, data string) string
- func HashCrc32(str string) uint32
- func HmacSha256(message string, secret string) string
- func IsIntranet(ipStr string) bool
- func JsonToMap(json string) (map[string]interface{}, error)
- func MD5(str string) string
- func MapToQueryParamSort(params map[string]interface{}) string
- func ObjToStr(v interface{}) string
- func PKCS5Padding(ciphertext []byte, blockSize int) []byte
- func PKCS5UnPadding(origData []byte) []byte
- func PKCS7Padding(ciphertext []byte, blockSize int) []byte
- func PKCS7UnPadding(origData []byte) []byte
- func PareTimeStrForYYYYMMdd(timeStr string) (time.Time, error)
- func PareTimeStrForYYYY_mm_dd(timeStr string) (time.Time, error)
- func ReadJsonByByte(body []byte, obj interface{}) error
- func RemoveRepeatedElement(arr []string) (newArr []string)
- func SHA1(str string) string
- func Sign(params map[string]interface{}, appKey string) string
- func Substr(str string, start, length int) string
- func Ten2Hex(ten int64) string
- func ToJson(obj interface{}) string
- func ToPageNumOrDefault(pageIndex string, pageSize string) (pIndex64 uint64, pSize64 uint64)
- func ToyyyyMM2(tm time.Time) string
- func ToyyyyMMdd(tm time.Time) string
- func ToyyyyMMddHHmm(tm time.Time) string
- func ToyyyyMMddHHmmss(tm time.Time) string
- func Toyyyy_MM(tm time.Time) string
- func Toyyyy_MM_dd(tm time.Time) string
- func UnderscoreName(name string) string
- func YuanToCent(yuan float64) int64
- type Buffer
- type Decimal
- func Avg(first Decimal, rest ...Decimal) Decimal
- func Max(first Decimal, rest ...Decimal) Decimal
- func Min(first Decimal, rest ...Decimal) Decimal
- func New(value int64, exp int32) Decimal
- func NewFromBigInt(value *big.Int, exp int32) Decimal
- func NewFromFloat(value float64) Decimal
- func NewFromFloatWithExponent(value float64, exp int32) Decimal
- func NewFromString(value string) (Decimal, error)
- func RequireFromString(value string) Decimal
- func Sum(first Decimal, rest ...Decimal) Decimal
- func (d Decimal) Abs() Decimal
- func (d Decimal) Add(d2 Decimal) Decimal
- func (d Decimal) Ceil() Decimal
- func (d Decimal) Cmp(d2 Decimal) int
- func (d Decimal) Coefficient() *big.Int
- func (d Decimal) Div(d2 Decimal) Decimal
- func (d Decimal) DivRound(d2 Decimal, precision int32) Decimal
- func (d Decimal) Equal(d2 Decimal) bool
- func (d Decimal) Equals(d2 Decimal) bool
- func (d Decimal) Exponent() int32
- func (d Decimal) Float64() (f float64, exact bool)
- func (d Decimal) Floor() Decimal
- func (d *Decimal) GobDecode(data []byte) error
- func (d Decimal) GobEncode() ([]byte, error)
- func (d Decimal) GreaterThan(d2 Decimal) bool
- func (d Decimal) GreaterThanOrEqual(d2 Decimal) bool
- func (d Decimal) IntPart() int64
- func (d Decimal) LessThan(d2 Decimal) bool
- func (d Decimal) LessThanOrEqual(d2 Decimal) bool
- func (d Decimal) MarshalBinary() (data []byte, err error)
- func (d Decimal) MarshalJSON() ([]byte, error)
- func (d Decimal) MarshalText() (text []byte, err error)
- func (d Decimal) Mod(d2 Decimal) Decimal
- func (d Decimal) Mul(d2 Decimal) Decimal
- func (d Decimal) Neg() Decimal
- func (d Decimal) Pow(d2 Decimal) Decimal
- func (d Decimal) QuoRem(d2 Decimal, precision int32) (Decimal, Decimal)
- func (d Decimal) Rat() *big.Rat
- func (d Decimal) Round(places int32) Decimal
- func (d Decimal) RoundBank(places int32) Decimal
- func (d Decimal) RoundCash(interval uint8) Decimal
- func (d *Decimal) Scan(value interface{}) error
- func (d Decimal) Shift(shift int32) Decimal
- func (d Decimal) Sign() int
- func (d Decimal) String() string
- func (d Decimal) StringFixed(places int32) string
- func (d Decimal) StringFixedBank(places int32) string
- func (d Decimal) StringFixedCash(interval uint8) string
- func (d Decimal) StringScaled(exp int32) string
- func (d Decimal) Sub(d2 Decimal) Decimal
- func (d Decimal) Truncate(precision int32) Decimal
- func (d *Decimal) UnmarshalBinary(data []byte) error
- func (d *Decimal) UnmarshalJSON(decimalBytes []byte) error
- func (d *Decimal) UnmarshalText(text []byte) error
- func (d Decimal) Value() (driver.Value, error)
- type NullDecimal
- type NullUUID
- type Page
- type UUID
- func And(u1 UUID, u2 UUID) UUID
- func FromBytes(input []byte) (u UUID, err error)
- func FromBytesOrNil(input []byte) UUID
- func FromString(input string) (u UUID, err error)
- func FromStringOrNil(input string) UUID
- func NewV1() UUID
- func NewV2(domain byte) UUID
- func NewV3(ns UUID, name string) UUID
- func NewV4() UUID
- func NewV5(ns UUID, name string) UUID
- func Or(u1 UUID, u2 UUID) UUID
- func (u UUID) Bytes() []byte
- func (u UUID) MarshalBinary() (data []byte, err error)
- func (u UUID) MarshalText() (text []byte, err error)
- func (u *UUID) Scan(src interface{}) error
- func (u *UUID) SetVariant()
- func (u *UUID) SetVersion(v byte)
- func (u UUID) String() string
- func (u *UUID) UnmarshalBinary(data []byte) (err error)
- func (u *UUID) UnmarshalText(text []byte) (err error)
- func (u UUID) Value() (driver.Value, error)
- func (u UUID) Variant() uint
- func (u UUID) Version() uint
Constants ¶
const ( VariantNCS = iota VariantRFC4122 VariantMicrosoft VariantFuture )
UUID layout variants.
const ( DomainPerson = iota DomainGroup DomainOrg )
UUID DCE domains.
Variables ¶
var (
NamespaceDNS, _ = FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
NamespaceURL, _ = FromString("6ba7b811-9dad-11d1-80b4-00c04fd430c8")
NamespaceOID, _ = FromString("6ba7b812-9dad-11d1-80b4-00c04fd430c8")
NamespaceX500, _ = FromString("6ba7b814-9dad-11d1-80b4-00c04fd430c8")
)
Predefined namespace UUIDs.
var DivisionPrecision = 16
DivisionPrecision is the number of decimal places in the result when it doesn't divide exactly.
Example:
d1 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(3) d1.String() // output: "0.6666666666666667" d2 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(30000) d2.String() // output: "0.0000666666666667" d3 := decimal.NewFromFloat(20000).Div(decimal.NewFromFloat(3) d3.String() // output: "6666.6666666666666667" decimal.DivisionPrecision = 3 d4 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(3) d4.String() // output: "0.667"
var MarshalJSONWithoutQuotes = false
MarshalJSONWithoutQuotes should be set to true if you want the decimal to be JSON marshaled as a number, instead of as a string. WARNING: this is dangerous for decimals with many digits, since many JSON unmarshallers (ex: Javascript's) will unmarshal JSON numbers to IEEE 754 double-precision floating point numbers, which means you can potentially silently lose precision.
var Nil = UUID{}
The nil UUID is special form of UUID that is specified to have all 128 bits set to zero.
var Zero = New(0, 1)
Zero constant, to make computations faster.
Functions ¶
func AesDecrypt ¶
AesDecrypt AesDecrypt
func AesDecryptPkcs5 ¶
AesDecryptPkcs5 解密
func AesDecryptPkcs7 ¶
AesDecryptPkcs7 解密
func AesDecryptPkcs7Base64 ¶
AesDecryptPkcs7Base64 解密
func AesDecryptSimple ¶
AesDecryptSimple 解密
func AesEncrypt ¶
func AesEncrypt(origData []byte, key []byte, iv []byte, paddingFunc func([]byte, int) []byte) ([]byte, error)
AesEncrypt AesEncrypt
func AesEncryptPkcs5 ¶
AesEncryptPkcs5 加密PKCS5
func AesEncryptPkcs7 ¶
AesEncryptPkcs7 加密PKCS5
func AesEncryptPkcs7Base64 ¶
AesEncryptPkcs7Base64 加密PKCS5
func AesEncryptSimple ¶
AesEncryptSimple 加密
func AttrToUnderscore ¶
func AttrToUnderscore(st interface{}) []string
AttrToUnderscore 获取struct的所有属性并转为下划线模式
func GetClientPublicIP ¶
GetClientPublicIP 尽最大努力实现获取客户端公网 IP 的算法。 解析 X-Real-IP 和 X-Forwarded-For 以便于反向代理(nginx 或 haproxy)可以正常工作。
func GetCurve25519Key ¶
GetCurve25519Key GetCurve25519Key
func GetCurve25519KeypPair ¶
func GetCurve25519KeypPair() (Aprivate, Apublic [32]byte)
GetCurve25519KeypPair GetCurve25519KeypPair
func GetIPAddress ¶
GetIPAddress 通过IP获取地址
func GetSignStr ¶
func HmacSha256 ¶
func MapToQueryParamSort ¶
MapToQueryParamSort map 以 key1=value1 & key2=value2形式排序拼接
func PKCS5Padding ¶
PKCS5Padding PKCS5Padding
func PKCS7Padding ¶
PKCS7Padding PKCS7Padding
func ReadJsonByByte ¶
func RemoveRepeatedElement ¶
RemoveRepeatedElement 移除重复元素
func ToPageNumOrDefault ¶
ToPageNumOrDefault 将字符串转换为数字类型 如果字符串为空 则赋值分页默认参数
func ToyyyyMMdd ¶
func ToyyyyMMddHHmm ¶
func ToyyyyMMddHHmmss ¶
Types ¶
type Decimal ¶
type Decimal struct {
// contains filtered or unexported fields
}
Decimal represents a fixed-point decimal. It is immutable. number = value * 10 ^ exp
func Max ¶
Max returns the largest Decimal that was passed in the arguments.
To call this function with an array, you must do:
Max(arr[0], arr[1:]...)
This makes it harder to accidentally call Max with 0 arguments.
func Min ¶
Min returns the smallest Decimal that was passed in the arguments.
To call this function with an array, you must do:
Min(arr[0], arr[1:]...)
This makes it harder to accidentally call Min with 0 arguments.
func NewFromBigInt ¶
NewFromBigInt returns a new Decimal from a big.Int, value * 10 ^ exp
func NewFromFloat ¶
NewFromFloat converts a float64 to Decimal.
Example:
NewFromFloat(123.45678901234567).String() // output: "123.4567890123456" NewFromFloat(.00000000000000001).String() // output: "0.00000000000000001"
NOTE: some float64 numbers can take up about 300 bytes of memory in decimal representation. Consider using NewFromFloatWithExponent if space is more important than precision.
NOTE: this will panic on NaN, +/-inf
func NewFromFloatWithExponent ¶
NewFromFloatWithExponent converts a float64 to Decimal, with an arbitrary number of fractional digits.
Example:
NewFromFloatWithExponent(123.456, -2).String() // output: "123.46"
func NewFromString ¶
NewFromString returns a new Decimal from a string representation.
Example:
d, err := NewFromString("-123.45") d2, err := NewFromString(".0001")
func RequireFromString ¶
RequireFromString returns a new Decimal from a string representation or panics if NewFromString would have returned an error.
Example:
d := RequireFromString("-123.45") d2 := RequireFromString(".0001")
func (Decimal) Cmp ¶
Cmp compares the numbers represented by d and d2 and returns:
-1 if d < d2 0 if d == d2 +1 if d > d2
func (Decimal) Coefficient ¶
Coefficient returns the coefficient of the decimal. It is scaled by 10^Exponent()
func (Decimal) Div ¶
Div returns d / d2. If it doesn't divide exactly, the result will have DivisionPrecision digits after the decimal point.
func (Decimal) DivRound ¶
DivRound divides and rounds to a given precision i.e. to an integer multiple of 10^(-precision)
for a positive quotient digit 5 is rounded up, away from 0 if the quotient is negative then digit 5 is rounded down, away from 0
Note that precision<0 is allowed as input.
func (Decimal) Float64 ¶
Float64 returns the nearest float64 value for d and a bool indicating whether f represents d exactly. For more details, see the documentation for big.Rat.Float64
func (*Decimal) GobDecode ¶
GobDecode implements the gob.GobDecoder interface for gob serialization.
func (Decimal) GreaterThan ¶
GreaterThan (GT) returns true when d is greater than d2.
func (Decimal) GreaterThanOrEqual ¶
GreaterThanOrEqual (GTE) returns true when d is greater than or equal to d2.
func (Decimal) LessThanOrEqual ¶
LessThanOrEqual (LTE) returns true when d is less than or equal to d2.
func (Decimal) MarshalBinary ¶
MarshalBinary implements the encoding.BinaryMarshaler interface.
func (Decimal) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (Decimal) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface for XML serialization.
func (Decimal) QuoRem ¶
QuoRem does divsion with remainder d.QuoRem(d2,precision) returns quotient q and remainder r such that
d = d2 * q + r, q an integer multiple of 10^(-precision) 0 <= r < abs(d2) * 10 ^(-precision) if d>=0 0 >= r > -abs(d2) * 10 ^(-precision) if d<0
Note that precision<0 is allowed as input.
func (Decimal) Round ¶
Round rounds the decimal to places decimal places. If places < 0, it will round the integer part to the nearest 10^(-places).
Example:
NewFromFloat(5.45).Round(1).String() // output: "5.5" NewFromFloat(545).Round(-1).String() // output: "550"
func (Decimal) RoundBank ¶
RoundBank rounds the decimal to places decimal places. If the final digit to round is equidistant from the nearest two integers the rounded value is taken as the even number
If places < 0, it will round the integer part to the nearest 10^(-places).
Examples:
NewFromFloat(5.45).Round(1).String() // output: "5.4" NewFromFloat(545).Round(-1).String() // output: "540" NewFromFloat(5.46).Round(1).String() // output: "5.5" NewFromFloat(546).Round(-1).String() // output: "550" NewFromFloat(5.55).Round(1).String() // output: "5.6" NewFromFloat(555).Round(-1).String() // output: "560"
func (Decimal) RoundCash ¶
RoundCash aka Cash/Penny/öre rounding rounds decimal to a specific interval. The amount payable for a cash transaction is rounded to the nearest multiple of the minimum currency unit available. The following intervals are available: 5, 10, 15, 25, 50 and 100; any other number throws a panic.
5: 5 cent rounding 3.43 => 3.45 10: 10 cent rounding 3.45 => 3.50 (5 gets rounded up) 15: 10 cent rounding 3.45 => 3.40 (5 gets rounded down) 25: 25 cent rounding 3.41 => 3.50 50: 50 cent rounding 3.75 => 4.00 100: 100 cent rounding 3.50 => 4.00
For more details: https://en.wikipedia.org/wiki/Cash_rounding
func (Decimal) Shift ¶
Shift shifts the decimal in base 10. It shifts left when shift is positive and right if shift is negative. In simpler terms, the given value for shift is added to the exponent of the decimal.
func (Decimal) String ¶
String returns the string representation of the decimal with the fixed point.
Example:
d := New(-12345, -3) println(d.String())
Output:
-12.345
func (Decimal) StringFixed ¶
StringFixed returns a rounded fixed-point string with places digits after the decimal point.
Example:
NewFromFloat(0).StringFixed(2) // output: "0.00" NewFromFloat(0).StringFixed(0) // output: "0" NewFromFloat(5.45).StringFixed(0) // output: "5" NewFromFloat(5.45).StringFixed(1) // output: "5.5" NewFromFloat(5.45).StringFixed(2) // output: "5.45" NewFromFloat(5.45).StringFixed(3) // output: "5.450" NewFromFloat(545).StringFixed(-1) // output: "550"
func (Decimal) StringFixedBank ¶
StringFixedBank returns a banker rounded fixed-point string with places digits after the decimal point.
Example:
NewFromFloat(0).StringFixed(2) // output: "0.00" NewFromFloat(0).StringFixed(0) // output: "0" NewFromFloat(5.45).StringFixed(0) // output: "5" NewFromFloat(5.45).StringFixed(1) // output: "5.4" NewFromFloat(5.45).StringFixed(2) // output: "5.45" NewFromFloat(5.45).StringFixed(3) // output: "5.450" NewFromFloat(545).StringFixed(-1) // output: "550"
func (Decimal) StringFixedCash ¶
StringFixedCash returns a Swedish/Cash rounded fixed-point string. For more details see the documentation at function RoundCash.
func (Decimal) StringScaled ¶
StringScaled first scales the decimal then calls .String() on it. NOTE: buggy, unintuitive, and DEPRECATED! Use StringFixed instead.
func (Decimal) Truncate ¶
Truncate truncates off digits from the number, without rounding.
NOTE: precision is the last digit that will not be truncated (must be >= 0).
Example:
decimal.NewFromString("123.456").Truncate(2).String() // "123.45"
func (*Decimal) UnmarshalBinary ¶
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. As a string representation is already used when encoding to text, this method stores that string as []byte
func (*Decimal) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.
func (*Decimal) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface for XML deserialization.
type NullDecimal ¶
NullDecimal represents a nullable decimal with compatibility for scanning null values from the database.
func (NullDecimal) MarshalJSON ¶
func (d NullDecimal) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface.
func (*NullDecimal) Scan ¶
func (d *NullDecimal) Scan(value interface{}) error
Scan implements the sql.Scanner interface for database deserialization.
func (*NullDecimal) UnmarshalJSON ¶
func (d *NullDecimal) UnmarshalJSON(decimalBytes []byte) error
UnmarshalJSON implements the json.Unmarshaler interface.
type NullUUID ¶
NullUUID can be used with the standard sql package to represent a UUID value that can be NULL in the database
type Page ¶
type Page struct { PageSize uint64 `json:"page_size"` PageIndex uint64 `json:"page_index"` Total uint64 `json:"total"` Data interface{} `json:"data"` }
Page Page
type UUID ¶
type UUID [16]byte
UUID representation compliant with specification described in RFC 4122.
func FromBytes ¶
FromBytes returns UUID converted from raw byte slice input. It will return error if the slice isn't 16 bytes long.
func FromBytesOrNil ¶
FromBytesOrNil returns UUID converted from raw byte slice input. Same behavior as FromBytes, but returns a Nil UUID on error.
func FromString ¶
FromString returns UUID parsed from string input. Input is expected in a form accepted by UnmarshalText.
func FromStringOrNil ¶
FromStringOrNil returns UUID parsed from string input. Same behavior as FromString, but returns a Nil UUID on error.
func (UUID) MarshalBinary ¶
MarshalBinary implements the encoding.BinaryMarshaler interface.
func (UUID) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface. The encoding is the same as returned by String.
func (*UUID) Scan ¶
Scan implements the sql.Scanner interface. A 16-byte slice is handled by UnmarshalBinary, while a longer byte slice or a string is handled by UnmarshalText.
func (*UUID) SetVariant ¶
func (u *UUID) SetVariant()
SetVariant sets variant bits as described in RFC 4122.
func (UUID) String ¶
Returns canonical string representation of UUID: 上海信必达网络科技有限公司-xxxx-xxxx-xxxx-上海信必达网络科技有限公司xxxx.
func (*UUID) UnmarshalBinary ¶
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. It will return error if the slice isn't 16 bytes long.
func (*UUID) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface. Following formats are supported: "6ba7b810-9dad-11d1-80b4-00c04fd430c8", "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}", "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8"