Skip to content

Commit

Permalink
Added TransformIntToCurrency() method
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Sep 21, 2020
1 parent d5bc8d3 commit f49024a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ View the generated [documentation](https://pkg.go.dev/github.com/tonicpow/go-bsv
- [ConvertSatsToBSV()](currency.go)
- [FormatCentsToDollars()](currency.go)
- [TransformCurrencyToInt()](currency.go)
- [TransformIntToCurrency()](currency.go)
- Supported Currencies:
- USD
- Supported Providers:
Expand Down
11 changes: 11 additions & 0 deletions currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ func TransformCurrencyToInt(decimalValue float64, currency Currency) (int64, err
return 0, fmt.Errorf("currency %s cannot be transformed", currency.Name())
}

// TransformIntToCurrency will take the int and return a float value.
// Currently only supports USD and BSV
func TransformIntToCurrency(intValue int, currency Currency) (string, error) {
if currency == CurrencyDollars {
return FormatCentsToDollars(intValue), nil
} else if currency == CurrencyBitcoin {
return fmt.Sprintf("%8.8f", ConvertSatsToBSV(intValue)), nil
}
return "", fmt.Errorf("currency %s cannot be transformed", currency.Name())
}

// ConvertFloatToIntBSV converts the BSV float value to the sats value
func ConvertFloatToIntBSV(floatValue float64) int64 {

Expand Down
48 changes: 48 additions & 0 deletions currency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,51 @@ func ExampleConvertFloatToIntBSV() {
fmt.Printf("%d", val)
// Output:1001000000
}

// TestTransformIntToCurrency will test the method TransformIntToCurrency()
func TestTransformIntToCurrency(t *testing.T) {
t.Parallel()

// Create the list of tests
var tests = []struct {
integer int
currency Currency
expected string
expectedError bool
}{
{0, CurrencyDollars, "0.00", false},
{-1, CurrencyDollars, "-0.01", false},
{127, CurrencyDollars, "1.27", false},
{1274, CurrencyDollars, "12.74", false},
{1276, CurrencyDollars, "12.76", false},
{1270000, CurrencyDollars, "12700.00", false},
{127, CurrencyBitcoin, "0.00000127", false},
{123456789123, CurrencyBitcoin, "1234.56789123", false},
{111, 123, "", true},
}

// Test all
for _, test := range tests {
if output, err := TransformIntToCurrency(test.integer, test.currency); err == nil && test.expectedError {
t.Errorf("%s Failed: expected to throw an error, no error [%d] inputted [%s] currency", t.Name(), test.integer, test.currency.Name())
} else if err != nil && !test.expectedError {
t.Errorf("%s Failed: [%d] inputted, received: [%s] error [%s]", t.Name(), test.integer, output, err.Error())
} else if output != test.expected && !test.expectedError {
t.Errorf("%s Failed: [%d] inputted and [%s] expected, received: [%s]", t.Name(), test.integer, test.expected, output)
}
}
}

// BenchmarkTransformIntToCurrency benchmarks the method TransformIntToCurrency()
func BenchmarkTransformIntToCurrency(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = TransformIntToCurrency(1000, CurrencyDollars)
}
}

// ExampleTransformIntToCurrency example using TransformIntToCurrency()
func ExampleTransformIntToCurrency() {
val, _ := TransformIntToCurrency(1000, CurrencyDollars)
fmt.Printf("%s", val)
// Output:10.00
}

0 comments on commit f49024a

Please sign in to comment.