go --- decimal 四舍五入/去尾/进一保留两位小数 不足小数点后填充0

  go中使用decimal需要使用第三方库,一般使用github.com/shopspring/decimal
 
  但是在实际使用中,会出现当decimal为整数时(如 48.00),调用本身接口转换成string时,无效的0会被省略。

price, _ := decimal.NewFromString("48.00")
fmt.Println(price.String())

结果:48

  当我们需要显示为金额时,需要额外处理让其显示小数点后面的0,即当小数点后的0被忽略时,自行填充。

  • 当金额为整数时,小数点后填充2个0
  • 当金额有1位小数时,小数点后填充1个0
  • 当金额有2位小数时,小数点后不做填充
  • 当金额超过2位小数时,四舍五入/去尾/进一保留两位小数

  实现上面的要求,可以使用StringFixed进行格式化:

StringFixed()可以保留小数点后指定位数

func Test_StringFixed(t *testing.T) {
	amount1, _ := decimal.NewFromString("300012.2834")
	t.Log(amount1.StringFixed(4))

	amount2, _ := decimal.NewFromString("300012.2834")
	t.Log(amount2.StringFixed(3))

	amount3, _ := decimal.NewFromString("300012.2834")
	t.Log(amount3.StringFixed(2))

	amount4, _ := decimal.NewFromString("300012.2834")
	t.Log(amount4.StringFixed(1))

	amount5, _ := decimal.NewFromString("300012.2834")
	t.Log(amount5.StringFixed(0))

	amount6, _ := decimal.NewFromString("300012.2834")
	t.Log(amount6.StringFixed(-1))
}

输出:
在这里插入图片描述
 

示例一:
  超过2位四舍五入,其余补0

Round()可以四舍五入到指定位数,直接使用StringFixed()也会自动四舍五入

func Test_Round(t *testing.T) {
  amount1, _ := decimal.NewFromString("300000.2834")
  t.Log(amount1.Round(2).StringFixed(2))

  amount2, _ := decimal.NewFromString("300000.2864")
  t.Log(amount2.Round(2).StringFixed(2))

  amount3, _ := decimal.NewFromString("300000.2")
  t.Log(amount3.Round(2).StringFixed(2))

  amount4, _ := decimal.NewFromString("300000")
  t.Log(amount4.Round(2).StringFixed(2))

  t.Log("--- StringFixed ---")

  amount5, _ := decimal.NewFromString("300000.2834")
  t.Log(amount5.StringFixed(2))

  amount6, _ := decimal.NewFromString("300000.2864")
  t.Log(amount6.StringFixed(2))

  amount7, _ := decimal.NewFromString("300000.2")
  t.Log(amount7.StringFixed(2))

  amount8, _ := decimal.NewFromString("300000")
  t.Log(amount8.StringFixed(2))
}

输出:
在这里插入图片描述
 
示例二:
  超过2位去尾,其余补0

RoundDown()可以对指定位数进行去尾

func Test_RoundDown(t *testing.T) {
	amount1, _ := decimal.NewFromString("300000.2834")
	t.Log(amount1.RoundDown(2).StringFixed(2))

	amount2, _ := decimal.NewFromString("300000.2864")
	t.Log(amount2.RoundDown(2).StringFixed(2))

	amount3, _ := decimal.NewFromString("300000.2")
	t.Log(amount3.RoundDown(2).StringFixed(2))

	amount4, _ := decimal.NewFromString("300000")
	t.Log(amount4.StringFixed(2))
}

输出
在这里插入图片描述

 
示例三:
  超过2位进一,其余补0

RoundUp()可以对指定位数进行去尾

func Test_RoundUp(t *testing.T) {
  amount1, _ := decimal.NewFromString("300000.2834")
  t.Log(amount1.RoundUp(2).StringFixed(2))

  amount2, _ := decimal.NewFromString("300000.2864")
  t.Log(amount2.RoundUp(2).StringFixed(2))

  amount3, _ := decimal.NewFromString("300000.2")
  t.Log(amount3.RoundUp(2).StringFixed(2))

  amount4, _ := decimal.NewFromString("300000")
  t.Log(amount4.RoundUp(2).StringFixed(2))
}

输出
在这里插入图片描述

 
参考链接:
https://blog.miuyun.work/archives/15302122

如有不对,烦请指出,感谢~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想在 `el-input` 组件中使用 `replace` 方法来限制只能输入数字、负数,并且小数点后最多保留四位小数,并且小数点后不能再输入小数点,你可以监听 `input` 事件,并在事件处理函数中进行相应的处理。下面是一个示例代码: ```html <template> <el-input v-model="inputValue" @input="handleInput"></el-input> </template> <script> export default { data() { return { inputValue: '' }; }, methods: { handleInput() { // 替换非数字、非小数点、非负号的字符为空字符串 this.inputValue = this.inputValue.replace(/[^\d.-]/g, ''); // 限制小数点后最多四位小数 const decimalIndex = this.inputValue.indexOf('.'); if (decimalIndex !== -1) { const decimalPart = this.inputValue.substr(decimalIndex + 1); if (decimalPart.length > 4) { this.inputValue = this.inputValue.substr(0, decimalIndex + 5); } } // 移除多余的小数点 const dotCount = (this.inputValue.match(/\./g) || []).length; if (dotCount > 1) { this.inputValue = this.inputValue.replace(/\.$/g, ''); } } } }; </script> ``` 在这个示例中,我们在 `el-input` 组件上绑定了 `v-model`,将输入框的值与 `inputValue` 数据进行双向绑定。同时,我们监听了 `input` 事件,并在事件处理函数 `handleInput` 中对用户输入进行处理。 在 `handleInput` 方法中,我们首先使用正则表达式替换非数字、非小数点、非负号的字符为空字符串。然后,检查是否存在小数点,并且如果小数部分的长度超过了四位,则进行截断。最后,我们通过正则表达式匹配多余的小数点,并将其移除。 这样,用户在 `el-input` 文本框中输入的值就会自动进行限制,只能是数字、负数,并且小数点后最多保留四位小数,并且小数点后不能再输入小数点
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值