Java 對負數進行四捨五入運算

2021-08-29 16:05:45 字數 502 閱讀 2657

對負數進行 round() 運算時,當絕對值小於0.5,直接返回0; 

如果絕對值大於0.5,先+0.5,然後做floor()運算。

下面是原始碼:

public static int round(float a) {

// greatest float value less than 0.5

if (a != 0x1.fffffep-2f)

return (int)floor(a + 0.5f);

else

return 0;

float f = -9.4f;

int i = math.round(f);

float g = -9.5f;

int j = math.round(g);

float h = -9.6f;

int k = math.round(g);

執行結果,i和j的值都是 -9,k的值為 -10,floor(-9.1)的值為-10

java對double型數字進行四捨五入

在之前的小測試中遇到需要對double型數字進行四捨五入操作 於是在網路上得到了解決方案 在此感謝各位前輩的記敘 呵呵 double unit 12.75 system.out.println new bigdecimal unit setscale 2,bigdecimal.round half ...

qt的四捨五入 Qt(C )四捨五入

qt現在是四捨六入五成雙,要想四捨五入得自己想辦法,實現如下 include include double c1 3.435 double c2 3.445 double c3 3.4351 double c4 3.4451 double c5 3.445 qdebug qdebug qdebug ...

mysql 金額 四捨五入 mysql四捨五入

雲資料庫 mysql 雲資料庫 rds for mysql 是穩定可靠 可彈性伸縮的雲資料庫服務。通過雲資料庫能夠讓您幾分鐘內完成資料庫部署。雲端完全託管,讓您專注於應用程式開發,無需為資料庫運維煩惱 低至 0.24 小時起,買1年83折 雲資料庫mysql 雲資料庫 rds for mysql 是...