【发布时间】:2021-03-31 01:21:22
【问题描述】:
我想检查 float32 是否有两位小数。我的 javascript 方法如下:
step := 0.01
value := 9.99
if int(value/step) % 1 == 0 {
printf("has two decimal places!")
}
上面的例子也有效。但是,当 step 不正确时,它将无法正常工作,然后无法正确从 float64 转换为 int。
例子:
step := 0.1
value := 9.99
if int(value/step) % 1 == 0 {
printf("has two decimal places!")
}
编译器错误:constant 9.99 truncated to integer
当我们使用动态值时,它只会为每种情况返回 true。
那么计算小数位数的正确方法是什么?
【问题讨论】:
标签: casting go floating-point-precision