问题描述
我想在 Go 中将像“12.49”这样的字符串解析为 *big.Int
。结果 *big.Int
应表示给定值中的美分数量,在本例中为 1249。以下是输入及其预期输出的更多示例:
- “3”:300
- “3.1”:310
- ".19": 19
我已经尝试使用 *big.Float
和 its Int
function,但意识到 *big.Float
不能提供任意精度。
现在我正在使用这个算法,但它看起来很脆弱 (Go Playground link):
func eurToCents(in string) *big.Int {
missingZerosUntilCents := 2
i := strings.Index(in,".")
if i > -1 {
missingZerosUntilCents -= len(in) - i - 1
if missingZerosUntilCents < 0 {
panic("too many decimal places")
}
}
in = strings.Replace(in,".","",1)
in += strings.Repeat("0",missingZerosUntilCents)
out,ok := big.NewInt(0).SetString(in,10)
if !ok {
panic(fmt.Sprintf("could not parse '%s' as an interger",in))
}
return out
}
Go 中是否有标准库函数或其他常用方法来解析货币?外部库不是一种选择。
PS:我正在解析 Nano cryptocurrency 值,它有 30 个小数位,最大值为 133,248,297.0。这就是我要求 *big.Int
而不是 uint64
的原因。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)