• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang money.Money类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/jbowles/money.Money的典型用法代码示例。如果您正苦于以下问题:Golang Money类的具体用法?Golang Money怎么用?Golang Money使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Money类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: TestMoneyFormatUSD

func TestMoneyFormatUSD(t *testing.T) {
	symbolUSD := "$"
	codeUSD := "USD"
	valStrExpect := "1234.56"
	valStr2Expect := "1234,56"
	val := int64(123456)
	val2 := float64(1234.56)

	mon := money.Money{M: 123456}
	mf, _ := mon.Format("USD")

	if mf.Symbol != symbolUSD {
		t.Error("wanted '$' but got", mf.Symbol)
	}
	if mf.IsoCode != codeUSD {
		t.Error("wanted 'USD' but got", mf.IsoCode)
	}
	if mf.MoneyVal.StringD() != valStrExpect {
		t.Error("wanted '1234.56' but got", mf.MoneyVal.StringD())
	}
	if mf.MoneyVal.StringC() != valStr2Expect {
		t.Error("wanted '1234,56' but got", mf.MoneyVal.StringC())
	}
	if mf.MoneyVal.Valuei() != val {
		t.Error("wanted int64 '123456' but got", mf.MoneyVal.Valuei())
	}
	if mf.MoneyVal.Valuef() != val2 {
		t.Error("wanted float64 '123456' but got", mf.MoneyVal.Valuef())
	}
}
开发者ID:zuzmic,项目名称:money,代码行数:30,代码来源:format_test.go


示例2: TestMoneyUpdateIsMoneyType

func TestMoneyUpdateIsMoneyType(t *testing.T) {
	m := money.Money{}
	var val1 int64 = 7868
	res := m.Updatei(val1)

	if res.Valuei() != val1 {
		t.Error("should be '7868'", res.Valuei())
	}

	typ := fmt.Sprintf("%T", m)
	typPointer := fmt.Sprintf("%T", res)

	printValue := fmt.Sprintf("%v", m.M)

	if typ != "money.Money" {
		t.Error("should be money type, instead it is: ", typ)
	}

	if typPointer != "*money.Money" {
		t.Error("should be money type, instead it is: ", typPointer)
	}

	if printValue != "7868" {
		t.Error("default print value for money Updatei() int64 '7868' should be '7868', but got", printValue)
	}

	printValued := fmt.Sprintf("%d", m)
	//if printValued != "{7868 0}" {
	if printValued != "{7868}" {
		//t.Error("base 10 print format of money struct should be base value '{7868 0}'", printValued)
		t.Error("base 10 print format of money struct should be base value '{7868}'", printValued)
	}
}
开发者ID:zuzmic,项目名称:money,代码行数:33,代码来源:money_type_mutable_test.go


示例3: TestMoneyVarsNotChanging

func TestMoneyVarsNotChanging(t *testing.T) {
	var val1 int64 = 67
	var val2 int64 = 6700
	m1 := money.Money{M: val1}
	m2 := money.Money{M: val2}
	if m1.Valuei() != val1 {
		t.Error("expected '67' got: ", m1.Valuei())
	}

	if m2.Valuei() != val2 {
		t.Error("expected '6700' got: ", m2.Valuei())
	}
}
开发者ID:zuzmic,项目名称:money,代码行数:13,代码来源:money_type_mutable_test.go


示例4: TestMoneyFormatBRL

func TestMoneyFormatBRL(t *testing.T) {
	symbolBRL := "R$"
	codeBRL := "BRL"
	//valStrExpect := "R$1.234,56 BRL"

	mon := money.Money{M: 123456}
	mf, _ := mon.Format("BRL") // "pt-BR" also works

	if mf.Symbol != symbolBRL {
		t.Error("wanted 'R$' but got", mf.Symbol)
	}
	if mf.IsoCode != codeBRL {
		t.Error("wanted 'BRL' but got", mf.IsoCode)
	}
}
开发者ID:zuzmic,项目名称:money,代码行数:15,代码来源:format_test.go


示例5: TestMoneyUpdateChangesOriginalValue

func TestMoneyUpdateChangesOriginalValue(t *testing.T) {
	m := money.Money{M: 67}
	m.Updatei(int64(6700))
	if m.Valuei() != int64(6700) {
		t.Error("original vlaue should be updated to '67.00' but got: ", m.Valuei())
	}
}
开发者ID:zuzmic,项目名称:money,代码行数:7,代码来源:money_type_mutable_test.go


示例6: TestMoneyDivLargeDenom

// division with a large denominator
func TestMoneyDivLargeDenom(t *testing.T) {
	m1 := money.Money{M: 67}   //67 cents!!
	m2 := money.Money{M: 6700} // 67 dollars
	res := m1.Div(&m2)
	finResi := int64(1)
	finResf := float64(0.01)

	if res.Valuei() != finResi {
		t.Error("expected '1' got: ", res.Valuei())
	}

	if res.Valuef() != finResf {
		t.Error("expected '0.01' got: ", res.Valuef())
	}

	if res.StringD() != "0.01" {
		t.Error("expected '100' got: ", res.StringD())
	}
}
开发者ID:zuzmic,项目名称:money,代码行数:20,代码来源:money_basic_ops_test.go


示例7: TestMoneyAddLarge

// adding large numbers floats via an `Updatef`
func TestMoneyAddLarge(t *testing.T) {
	var val1 float64 = 12390678659.32 //12390678659.32
	var val2 float64 = 8937670084.36  // 8937670084.36
	m1 := money.Money{}
	m2 := money.Money{}
	m1Set := m1.Updatef(val1)
	m2Set := m2.Updatef(val2)
	res := m1Set.Add(m2Set)

	//12390678659.32 + 8937670084.36 = 21,328,348,743.68
	if res.Valuef() != float64(21328348743.68) {
		t.Error("expected negaitve '21328348743.68' got: ", res.Valuef())
	}

	if res.Valuei() != int64(2132834874368) {
		t.Error("expected negaitve '2132834874368' got: ", res.Valuei())
	}

	if res.StringD() != "21328348743.68" {
		t.Error("expected '21328348743.68' got: ", res.StringD())
	}

	//12390678659.32 + 8937670084.36 = 21,328,348,743.68
	if res.StringC() != "21328348743,68" {
		t.Error("expected '21328348743,68' got: ", res.StringC())
	}
}
开发者ID:zuzmic,项目名称:money,代码行数:28,代码来源:money_basic_ops_test.go


示例8: TestMoneyOverflow

func TestMoneyOverflow(t *testing.T) {
	flag.Parse()
	if ovf == 0 {
		t.Skip("Skip overflow no 'overflow' cli flag set: 'ovf=1 will run it'")
	}

	r := random(1, 25)
	m1 := money.Money{M: money.MaxInt}
	m2 := money.Money{M: money.MinInt}
	if (r % 2) == 1 {
		_ = m1.Add(&m1)
	} else {
		_ = m2.Sub(&m1)
	}

	/*
		** this was testing using fields on the struct to check for overlfow errors
		// overflow error should return value of struct attempting op
		if res.Valuei() != int64(0) {
			t.Error("expected overflow value to be zero", res.Valuei())
		}

		if res.OvfErr != true {
			t.Error("expected ok to be false", res.OvfErr)
		}

		if len(res.Ovf) < 1 {
			t.Error("need overflow message", res.Ovf)
		}
	*/
}
开发者ID:zuzmic,项目名称:money,代码行数:31,代码来源:money_basic_ops_test.go


示例9: TestMoneySmallNumberSubtractLarger

func TestMoneySmallNumberSubtractLarger(t *testing.T) {
	var val1 int64 = 67   //0.67
	var val2 int64 = 6700 //67.00
	m1 := money.Money{}
	m2 := money.Money{}
	m1Set := m1.Updatei(val1)
	m2Set := m2.Updatei(val2)

	res := m1Set.Sub(m2Set)

	if res.Valuei() != int64(-6633) {
		t.Error("expected negative '-6633' got: ", res.Valuei())
	}

	if res.Valuef() != float64(-66.33) {
		t.Error("expected negative '-66.33' got: ", res.Valuef())
	}
}
开发者ID:zuzmic,项目名称:money,代码行数:18,代码来源:money_basic_ops_test.go


示例10: TestMoneyStringWithUpdate

func TestMoneyStringWithUpdate(t *testing.T) {
	m := money.Money{M: 67}
	if m.StringD() != "0.67" {
		t.Error("wanted to see '0.67' cents but got: ", m.StringD())
	}

	var val2 int64 = 6700
	m.Updatei(val2)
	if m.StringD() != "67.00" {
		t.Error("wanted to see '67.00' dollars but got: ", m.StringD())
	}
}
开发者ID:zuzmic,项目名称:money,代码行数:12,代码来源:money_type_mutable_test.go


示例11: TestMoneyMul

// check multiplication is good but also that original values are not modified
func TestMoneyMul(t *testing.T) {
	m1 := money.Money{M: 67}   //67 cents!!
	m2 := money.Money{M: 6700} // 67 dollars
	res := m2.Mul(&m1)
	finResi := int64(4489)    // 44 dollars and 89 cents
	finResf := float64(44.89) // 44 dollars and 89 cents

	if res.Valuei() != finResi {
		t.Error("expected '4489' got: ", res.Valuei())
	}

	if res.Valuef() != finResf {
		t.Error("expected '44.89' got: ", res.Valuef())
	}

	if m1.Valuei() != int64(67) {
		t.Error("expected '67' got: ", m1.Valuei())
	}

	if m2.Valuei() != int64(6700) {
		t.Error("expected '6700' got: ", m2.Valuei())
	}

	if m1.Valuef() != float64(0.67) {
		t.Error("expected '0.67' got: ", m1.Valuef())
	}

	if m2.Valuef() != float64(67.00) {
		t.Error("expected '67.00' got: ", m2.Valuef())
	}

	if res.StringD() != "44.89" {
		t.Error("expected '44.89' got: ", res.StringD())
	}
}
开发者ID:zuzmic,项目名称:money,代码行数:36,代码来源:money_basic_ops_test.go


示例12: TestMoneySub

// check subtraction is good but also that original values are not modified
func TestMoneySub(t *testing.T) {
	m1 := money.Money{M: 67}
	m2 := money.Money{M: 6700}
	res := m2.Sub(&m1)
	finResi := int64(6633)
	finResf := float64(66.33)

	if res.Valuei() != finResi {
		t.Error("expected '6633' got: ", res.Valuei())
	}

	if res.Valuef() != finResf {
		t.Error("expected '66.33' got: ", res.Valuef())
	}

	if m1.Valuei() != int64(67) {
		t.Error("expected '67' got: ", m1.Valuei())
	}

	if m2.Valuei() != int64(6700) {
		t.Error("expected '6700' got: ", m2.Valuei())
	}

	if m1.Valuef() != float64(0.67) {
		t.Error("expected '0.67' got: ", m1.Valuef())
	}

	if m2.Valuef() != float64(67.00) {
		t.Error("expected '67.00' got: ", m2.Valuef())
	}

	if res.StringD() != "66.33" {
		t.Error("expected '66.33' got: ", res.StringD())
	}
}
开发者ID:zuzmic,项目名称:money,代码行数:36,代码来源:money_basic_ops_test.go


示例13: TestMoneyNegativeSubUpdate

// verify subtracting negatives with updates does UPDATE and change original values
// and returns correct results
func TestMoneyNegativeSubUpdate(t *testing.T) {
	var val1Neg int64 = -1   //-0.01 cents
	var val2Pos int64 = 6700 //-67.00 dollars
	m1Neg := money.Money{}
	m2Pos := money.Money{}
	m1SetNeg := m1Neg.Updatei(val1Neg)
	m2SetPos := m2Pos.Updatei(val2Pos)
	resNeg := m1SetNeg.Sub(m2SetPos)

	vali := resNeg.Valuei()
	valf := resNeg.Valuef()

	if vali != int64(-6701) {
		t.Error("expected negative '-6701' got: ", vali)
	}

	if valf != float64(-67.01) {
		t.Error("expected negative '-67.01' got: ", valf)
	}

	// verify return values from update were set correctly
	if m1SetNeg.Valuei() != val1Neg {
		t.Error("expected negative '-1' got: ", m1SetNeg.Valuei())
	}

	if m2SetPos.Valuei() != val2Pos {
		t.Error("expected negative '6700' got: ", m2SetPos.Valuei())
	}

	// verify original values for an update were updated
	if m1Neg.Valuei() != val1Neg {
		t.Error("expected negative '-1' got: ", m1Neg.Valuei())
	}

	if m2Pos.Valuei() != val2Pos {
		t.Error("expected negative '6700' got: ", m2Pos.Valuei())
	}
}
开发者ID:zuzmic,项目名称:money,代码行数:40,代码来源:money_basic_ops_test.go


示例14: TestMoneyDiv

// check division is good but also that original values are not modified
func TestMoneyDiv(t *testing.T) {
	m1 := money.Money{M: 67}   //67 cents!!
	m2 := money.Money{M: 6700} // 67 dollars
	res := m2.Div(&m1)
	finResi := int64(10000)
	finResf := float64(100.00)

	if res.Valuei() != finResi {
		t.Error("expected '10000' got: ", res.Valuei())
	}

	if res.Valuef() != finResf {
		t.Error("expected '100.00' got: ", res.Valuef())
	}

	if m1.Valuei() != int64(67) {
		t.Error("expected '67' got: ", m1.Valuei())
	}

	if m2.Valuei() != int64(6700) {
		t.Error("expected '6700' got: ", m2.Valuei())
	}

	if m1.Valuef() != float64(0.67) {
		t.Error("expected '0.67' got: ", m1.Valuef())
	}

	if m2.Valuef() != float64(67.00) {
		t.Error("expected '67.00' got: ", m2.Valuef())
	}

	if res.StringD() != "100.00" {
		t.Error("expected '100.00' got: ", res.StringD())
	}
}
开发者ID:zuzmic,项目名称:money,代码行数:36,代码来源:money_basic_ops_test.go


示例15: TestMoneyValueIntAndFloat

func TestMoneyValueIntAndFloat(t *testing.T) {
	val := money.Money{M: 123456}

	if val.Valuei() != int64(123456) {
		t.Error("Valuei() should be int64 '123456'", val.Valuei())
	}

	if val.Valuef() != float64(1234.56) {
		t.Error("Valuef() should be float64 '123456'", val.Valuef())
	}

	if val.ValueiTrunc() != int64(1234) {
		t.Error("ValueiTrunc() should be int64 '1234'", val.ValueiTrunc())
	}

	if val.StringD() != "1234.56" {
		t.Error("money struct init StringD() should be value '1234.56'", val.StringD())
	}

	if val.StringC() != "1234,56" {
		t.Error("money struct init StringC() should be value '1234,56'", val.StringC())
	}
}
开发者ID:zuzmic,项目名称:money,代码行数:23,代码来源:money_type_mutable_test.go


示例16: TestMoneyNegNotMutable

// Neg value make sure 'val' is not updated
func TestMoneyNegNotMutable(t *testing.T) {
	val := money.Money{M: 123456}
	neg := val.Neg()

	if val.Valuei() != int64(123456) {
		t.Error("val should be int64 '123456'", val.Valuei())
	}

	if val.Valuef() != float64(1234.56) {
		t.Error("val should be int64 '1234.56'", val.Valuef())
	}

	if neg.Valuei() != int64(-123456) {
		t.Error("neg.Valuei should be int64 '-123456'", neg.Valuei())
	}

	if neg.Valuef() != float64(-1234.56) {
		t.Error("neg.Valuef should be float64 '-1234.56'", neg.Valuef())
	}

	if val.StringD() != "1234.56" {
		t.Error("val.StringD() should be '1234.56'", val.StringD())
	}
	// TODO oddity for negative money values!!!!
	if neg.StringD() != "-1234.56" {
		t.Error("neg.StringD() should be '-1234.56'", neg.StringD())
	}
}
开发者ID:zuzmic,项目名称:money,代码行数:29,代码来源:money_type_mutable_test.go


示例17: TestMoneyDivByZero

func TestMoneyDivByZero(t *testing.T) {
	m1 := money.Money{}
	m2 := money.Money{M: int64(1)}
	m3 := money.Money{M: int64(72684839824903281)}
	m4 := money.Money{}
	m4.Updatef(float64(478326437489327489327838462381))

	res1 := m2.Div(&m1)
	res2 := m3.Div(&m1)
	res3 := m4.Div(&m1)

	if res1.Valuei() != int64(-9223372036854775807) {
		t.Error("expected '-9223372036854775807' got: ", res1.Valuei())
	}

	if res2.Valuei() != int64(-9223372036854775807) {
		t.Error("expected '-9223372036854775807' got: ", res2.Valuei())
	}

	if res3.Valuei() != int64(9223372036854775807) {
		t.Error("expected '9223372036854775807' got: ", res3.Valuei())
	}

	if res1.Valuef() != float64(-9.223372036854776e+16) {
		t.Error("expected '-9223372036854775807' got: ", res1.Valuef())
	}

	if res2.Valuef() != float64(-9.223372036854776e+16) {
		t.Error("expected '-9223372036854775807' got: ", res2.Valuef())
	}

	if res3.Valuef() != float64(9.223372036854776e+16) {
		t.Error("expected '9223372036854775807' got: ", res3.Valuef())
	}
}
开发者ID:zuzmic,项目名称:money,代码行数:35,代码来源:money_basic_ops_test.go


示例18: TestMoneyAbs

func TestMoneyAbs(t *testing.T) {
	v1 := int64(7839)
	v2 := float64(38261748.09)
	m1 := money.Money{M: -v1}
	m2 := money.Money{}
	m2.Updatef(-v2)

	m1abs := m1.Abs()
	m2abs := m2.Abs()

	m1pos := money.Money{M: v1}
	m2pos := money.Money{}
	m2pos.Updatef(v2)

	if m1abs.Valuei() != m1pos.Valuei() {
		t.Error("expected '7839' got: ", m1abs.Valuei())
	}

	if m2abs.Valuei() != m2pos.Valuei() {
		t.Error("expected '3826174809' got: ", m2abs.Valuei())
	}

	if m1.Abs().Valuei() != m1pos.Valuei() {
		t.Error("expected '7839' got: ", m1.Abs().Valuei())
	}

	if m2.Abs().Valuei() != m2pos.Valuei() {
		t.Error("expected '3826174809' got:", m2.Abs().Valuei())
	}

	//verify original values are still negative
	if m1.Valuei() != -m1pos.Valuei() {
		t.Error("expected '-7839' got: ", m1.Valuei())
	}

	if m2.Valuei() != -m2pos.Valuei() {
		t.Error("expected '-3826174809' got: ", m2.Valuei())
	}
}
开发者ID:zuzmic,项目名称:money,代码行数:39,代码来源:money_basic_ops_test.go


示例19: TestMoneyAdd

// check addition is good but also that original values are not modified
func TestMoneyAdd(t *testing.T) {
	m1 := money.Money{M: int64(67)}
	m2 := money.Money{M: int64(6700)}
	res := m1.Add(&m2)
	finResi := int64(6767)
	finResf := float64(67.67)

	if res.Valuei() != finResi {
		t.Error("expected '6767' got: ", res.Valuei())
	}

	if res.Valuef() != finResf {
		t.Error("expected '6767' got: ", res.Valuef())
	}

	if m1.Valuei() != int64(67) {
		t.Error("expected '67' got: ", m1.Valuei())
	}

	if m2.Valuei() != int64(6700) {
		t.Error("expected '6700' got: ", m2.Valuei())
	}

	if m1.Valuef() != float64(0.67) {
		t.Error("expected '0.67' got: ", m1.Valuef())
	}

	if m2.Valuef() != float64(67.00) {
		t.Error("expected '67.00' got: ", m2.Valuef())
	}

	if res.StringD() != "67.67" {
		t.Error("expected '67.67' got: ", res.StringD())
	}

	if res.StringC() != "67,67" {
		t.Error("expected '67,67' got: ", res.StringC())
	}
}
开发者ID:zuzmic,项目名称:money,代码行数:40,代码来源:money_basic_ops_test.go


示例20: BenchmarkFormatUSD

func BenchmarkFormatUSD(b *testing.B) {
	mon := money.Money{M: 123456}
	for i := 0; i < b.N; i++ {
		_, _ = mon.Format("USD")
	}
}
开发者ID:zuzmic,项目名称:money,代码行数:6,代码来源:format_test.go



注:本文中的github.com/jbowles/money.Money类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang xml.Node类代码示例发布时间:2022-05-23
下一篇:
Golang maggiefs.Inode类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap