Documentation ¶
Overview ¶
Package decint provides utility functions to parse and represent decimal values as integers with a set precision.
The functions in this package are typically used to store and retrieve small currency values without loss of precision.
Safe decimal values are limited up to 2^53 / 1e+6 = 9_007_199_254.740_992.
Index ¶
Examples ¶
Constants ¶
const ( // MaxInt is the maximum integer number that can be safely represented (2^53). MaxInt = 9_007_199_254_740_992 // MaxFloat is the maximum float number that can be safely represented (2^53 / 1e+06). MaxFloat = 9_007_199_254.740_992 )
Variables ¶
This section is empty.
Functions ¶
func FloatToInt ¶
FloatToInt Converts the value to a int64 representation.
Example ¶
package main import ( "fmt" "github.com/Vonage/gosrvlib/pkg/decint" ) func main() { v := decint.FloatToInt(123.456) fmt.Println(v) }
Output: 123456000
func FloatToUint ¶
FloatToUint Converts the value to a uint64 representation. Negative values are converted to zero.
Example ¶
package main import ( "fmt" "github.com/Vonage/gosrvlib/pkg/decint" ) func main() { v := decint.FloatToUint(123.456) fmt.Println(v) }
Output: 123456000
func IntToFloat ¶
IntToFloat Converts back the int64 representation into a float value.
Example ¶
package main import ( "fmt" "github.com/Vonage/gosrvlib/pkg/decint" ) func main() { v := decint.IntToFloat(123456) fmt.Println(v) }
Output: 0.123456
func IntToString ¶
IntToString format the int64 representation as string.
Example ¶
package main import ( "fmt" "github.com/Vonage/gosrvlib/pkg/decint" ) func main() { v := decint.IntToString(123456) fmt.Println(v) }
Output: 0.123456
func StringToInt ¶
StringToInt parse the input string float value and returns the int64 representation.
Example ¶
package main import ( "fmt" "log" "github.com/Vonage/gosrvlib/pkg/decint" ) func main() { v, err := decint.StringToInt("123456") if err != nil { log.Fatal(err) } fmt.Println(v) }
Output: 123456000000
func StringToUint ¶
StringToUint parse the input string float value and returns the uint64 representation. Negative values are converted to zero.
Example ¶
package main import ( "fmt" "log" "github.com/Vonage/gosrvlib/pkg/decint" ) func main() { v, err := decint.StringToUint("123.456") if err != nil { log.Fatal(err) } fmt.Println(v) }
Output: 123456000
func UintToFloat ¶
UintToFloat Converts back the uint64 representation into a float value.
Example ¶
package main import ( "fmt" "github.com/Vonage/gosrvlib/pkg/decint" ) func main() { v := decint.UintToFloat(123456) fmt.Println(v) }
Output: 0.123456
func UintToString ¶
UintToString format the uint64 representation as string.
Example ¶
package main import ( "fmt" "github.com/Vonage/gosrvlib/pkg/decint" ) func main() { v := decint.UintToString(123456) fmt.Println(v) }
Output: 0.123456
Types ¶
This section is empty.