Tag: rounding

清除尾随0的双倍?

我有一个双倍,它有一个像0.50000的值,但我只想0.5 – 有没有办法摆脱那些尾随0? 🙂

printf双打的舍入行为

有人可以解释这种行为吗? 我很清楚浮点数的机器级表示。 这似乎与printf及其格式有关。 两个数字都用浮点表示法精确表示(检查:乘以64得到一个整数)。 #include #include using namespace std; int main() { double x1=108.765625; printf(“%34.30f\n”, x1); printf(“%9.5f\n”, x1); printf(“%34.30f\n”, x1*64); double x2=108.046875; printf(“%34.30lf\n”, x2); printf(“%9.5f\n”, x2); printf(“%34.30f\n”, x2*64); } 输出: > 108.765625000000000000000000000000 > 108.76562 > 6961.000000000000000000000000000000 > 108.046875000000000000000000000000 > 108.04688 > 6915.000000000000000000000000000000 请注意,第一个数字向下舍入,第二个数字向上舍入。