意思是第3位之后的數(shù)都舍棄嗎? 可以這樣, #include <stdio.h> int main() { double d1 = 1234.567; double d2 = d1 - ( int )d1; // 求出小數(shù)部分 int i = d2 * 1000; // 取出小數(shù)后3位 if (i%10 >=5 ) { i -= i%10; i += 10; // 四舍五入 } d1 = ( int )d1 + ( double )i / 1000; printf ( "d1 = %lf \n" , d1); return 0; } |
|