decint

package
v1.75.5 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2023 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package decint provides utility functions to parse and represent decimal values as integer with a set precision. Typically this is useful to store small currency values. Safe values are up to 2^53 / 1e+6 = 9_007_199_254.740_992

Index

Examples

Constants

View Source
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

func FloatToInt(v float64) int64

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

func FloatToUint(v float64) uint64

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

func IntToFloat(v int64) float64

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

func IntToString(v int64) string

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

func StringToInt(s string) (int64, error)

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

func StringToUint(s string) (uint64, error)

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

func UintToFloat(v uint64) float64

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

func UintToString(v uint64) string

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.

Jump to

Keyboard shortcuts

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