Skip to content

Commit

Permalink
Language based Humanized o/p of money value added.
Browse files Browse the repository at this point in the history
The old function StringHuman() has been removed because it was not respecting certain language based formatting.
  • Loading branch information
subwiz committed Dec 9, 2024
1 parent 2c31852 commit 530e2e2
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 193 deletions.
356 changes: 178 additions & 178 deletions conf/currency.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
type currTmp struct {
Code string `json:"code"`
Number uint `json:"number"`
Symbol string `json:"-"`
Symbol string `json:"sym"`
NOD uint `json:"nod"` // Number of Decimals
}

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/wiztools/moneylib

go 1.22
go 1.23

require github.com/joiggama/money v2.0.0+incompatible
require golang.org/x/text v0.21.0
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/joiggama/money v2.0.0+incompatible h1:ptJDA0ZZV701nSpAOvVyTVYb8ew4/L8uCDDyt7UGgwk=
github.com/joiggama/money v2.0.0+incompatible/go.mod h1:3lGjMzCembbQIbnmKz+pksaGBAQ9bMVSk1ZMsKM9e78=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
31 changes: 24 additions & 7 deletions money.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"strconv"
"strings"

"github.com/joiggama/money"
"golang.org/x/text/language"
"golang.org/x/text/message"
)

// Money type. Use NewMoney*() functions to instantiate.
Expand Down Expand Up @@ -46,14 +47,30 @@ func (m *Money) String() string {
return fmt.Sprintf("%v.%v", m.intPart, m.decPart)
}

// StringHuman returns money representation for humans.
func (m *Money) StringHuman() string {
curr := m.Currency()
f, _ := m.Float().Float64()
o := money.Format(f, money.Options{"currency": curr.Code()})
return o
func (m *Money) HumanEN() string {
return m.Human(language.English)
}

func (m *Money) Human(lan language.Tag) string {
p := message.NewPrinter(lan)
if m.Currency().nod == 0 {
return p.Sprintf("%s%d", m.curr.symbol, m.Whole())
}
// Format the money value to the exact decimal value defined
// for the currency:
format := fmt.Sprintf("%%.%df", m.Currency().nod)
value, _ := m.Float().Float64()
return p.Sprintf("%s"+format, m.curr.symbol, value)
}

// StringHuman returns money representation for humans.
// func (m *Money) StringHuman() string {
// curr := m.Currency()
// f, _ := m.Float().Float64()
// o := money.Format(f, money.Options{"currency": curr.Code()})
// return o
// }

func pow10(x uint) uint {
if x == 0 {
return 0
Expand Down
37 changes: 35 additions & 2 deletions money_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"math/big"
"testing"

"golang.org/x/text/language"
)

func TestString(t *testing.T) {
Expand All @@ -17,14 +19,45 @@ func TestString(t *testing.T) {
}
}

func TestHuman(t *testing.T) {
{
m, err := NewMoneyPart("INR", "1234567", "89")
if err != nil {
t.Error(err)
return
}
fmt.Println("INR", m.String(), m.Human(language.Italian), m.Human(language.English))
if m.Human(language.Italian) != "₹1.234.567,89" {
t.Fail()
}
if m.Human(language.English) != "₹1,234,567.89" {
t.Fail()
}
}
{
m, err := NewMoneyWhole("JPY", 1234567)
if err != nil {
t.Error(err)
return
}
fmt.Println("JPY", m.String(), m.Human(language.Italian), m.Human(language.English))
if m.Human(language.Italian) != "¥1.234.567" {
t.Fail()
}
if m.Human(language.English) != "¥1,234,567" {
t.Fail()
}
}
}

func TestStringHuman(t *testing.T) {
o, err := NewMoneyStr("INR", "122123.02")
if err != nil {
t.Error(err)
return
}
fmt.Printf("Human: %v.\n", o.StringHuman())
if o.StringHuman() != "₹122,123.02" {
fmt.Printf("Human: %v.\n", o.HumanEN())
if o.HumanEN() != "₹122,123.02" {
t.Fail()
}
}
Expand Down
2 changes: 1 addition & 1 deletion serializablemoney.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (m *Money) SerializableMoney() SerializableMoney {
func (m *Money) SerializableDispMoney() SerializableDispMoney {
var out SerializableDispMoney
out.Value = m.String()
out.Display = m.StringHuman()
out.Display = m.HumanEN()
curr := m.Currency()
out.Currency = curr.Code()
return out
Expand Down

0 comments on commit 530e2e2

Please sign in to comment.