Skip to content

Commit

Permalink
Add *big.Float support.
Browse files Browse the repository at this point in the history
  • Loading branch information
leekchan committed Aug 22, 2015
1 parent c6a4de0 commit ad542aa
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
language: go
go:
- 1.4.2
- 1.5
- tip
before_install:
- go get github.com/axw/gocov/gocov
Expand Down
15 changes: 15 additions & 0 deletions accounting15.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// +build go1.5

package accounting

import (
"math/big"
)

// FormatMoneyBigFloat only supports *big.Float value. It is faster than FormatMoney,
// because it does not do any runtime type evaluation.
func (accounting *Accounting) FormatMoneyBigFloat(value *big.Float) string {
accounting.init()
formattedNumber := FormatNumberBigFloat(value, accounting.Precision, accounting.Thousand, accounting.Decimal)
return accounting.formatMoneyString(formattedNumber)
}
26 changes: 26 additions & 0 deletions accounting15_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// +build go1.5

package accounting

import (
"math/big"
"testing"
)

func TestFormatMoneyBigFloat(t *testing.T) {
accounting := Accounting{Symbol: "$", Precision: 2}
AssertEqual(t, accounting.FormatMoneyBigFloat(big.NewFloat(123456789.213123)), "$123,456,789.21")

accounting = Accounting{Symbol: "€", Precision: 2, Thousand: ".", Decimal: ","}
AssertEqual(t, accounting.FormatMoneyBigFloat(big.NewFloat(4999.99)), "€4.999,99")
AssertEqual(t, accounting.FormatMoneyBigFloat(big.NewFloat(-4999.99)), "€-4.999,99")

accounting = Accounting{Symbol: "£ ", Precision: 0}
AssertEqual(t, accounting.FormatMoneyBigFloat(big.NewFloat(-500000.0)), "£ -500,000")

accounting = Accounting{Symbol: "GBP", Precision: 0,
Format: "%s %v", FormatNegative: "%s (%v)", FormatZero: "%s --"}
AssertEqual(t, accounting.FormatMoneyBigFloat(big.NewFloat(1000000.0)), "GBP 1,000,000")
AssertEqual(t, accounting.FormatMoneyBigFloat(big.NewFloat(-5000.0)), "GBP (5,000)")
AssertEqual(t, accounting.FormatMoneyBigFloat(big.NewFloat(0.0)), "GBP --")
}
6 changes: 6 additions & 0 deletions accounting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package accounting
import (
"math/big"
"testing"
"runtime"
"fmt"
)

func AssertEqual(t *testing.T, x, y string) {
Expand All @@ -11,6 +13,10 @@ func AssertEqual(t *testing.T, x, y string) {
}
}

func init() {
fmt.Printf("version: %s", runtime.Version())
}

func TestFormatMoney(t *testing.T) {
accounting := Accounting{Symbol: "$", Precision: 2}
AssertEqual(t, accounting.FormatMoney(123456789.213123), "$123,456,789.21")
Expand Down
13 changes: 13 additions & 0 deletions formatnumber15.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// +build go1.5

package accounting

import (
"math/big"
)

// FormatNumberBigFloat only supports *big.Float value.
// It is faster than FormatNumber, because it does not do any runtime type evaluation.
func FormatNumberBigFloat(x *big.Float, precision int, thousand string, decimal string) string {
return formatNumberString(x.Text('f', precision), precision, thousand, decimal)
}
17 changes: 17 additions & 0 deletions formatnumber15_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// +build go1.5

package accounting

import (
"math/big"
"testing"
)

func TestFormatNumberBigFloat(t *testing.T) {
AssertEqual(t, FormatNumberBigFloat(big.NewFloat(123456789.213123), 3, ",", "."), "123,456,789.213")
AssertEqual(t, FormatNumberBigFloat(big.NewFloat(-12345.123123), 5, ",", "."), "-12,345.12312")
AssertEqual(t, FormatNumberBigFloat(big.NewFloat(-1234.123123), 5, ",", "."), "-1,234.12312")
AssertEqual(t, FormatNumberBigFloat(big.NewFloat(-123.123123), 5, ",", "."), "-123.12312")
AssertEqual(t, FormatNumberBigFloat(big.NewFloat(-12.123123), 5, ",", "."), "-12.12312")
AssertEqual(t, FormatNumberBigFloat(big.NewFloat(-1.123123), 5, ",", "."), "-1.12312")
}

0 comments on commit ad542aa

Please sign in to comment.