Có bà con nào biết hàm làm tròn số trong JAVA ko làm ơn chỉ giúp.
VD:
1.256 -> 1.26
2.227 -> 2.23
v.v....
Có thể email trực tiếp cho mình: pop0811@softhome.net
Có bà con nào biết hàm làm tròn số trong JAVA ko làm ơn chỉ giúp.
VD:
1.256 -> 1.26
2.227 -> 2.23
v.v....
Có thể email trực tiếp cho mình: pop0811@softhome.net
Ban co the dung cac static method cua class Math:
Math.round() - lam tron so
Math.floor() - lam tron xuong
Math.ceil() - lam tron len
Than
Khong duoc roi bac oi, nhung ham nay khong cho lam tron co lay so le. Nho cac bac tim gium em cai ham nao lam tron co the lay duoc so le.
vd
1.235 -> 1.24 (lay 2 so le)
1.22456 -> 1.2246 (lay 4 so le)
Óe, bạn pop0811 học lập trình kiểu gì thế?
Vấn đề này quá đơn giản, có gì đâu nhỉ (mặc dù tôi mới học Java được mấy tháng thôi)
Thôi viết tặng bạn pop0811 hẳn một class theo như yêu cấu của bạn nhé
Để dùng method round của class này (có tên là MathForDummies), bạn chỉ cần nhập số cần làm tròn vá số chữ số sau dấu phẩy bạn muốn làm tròn đến.
Ví dụ MathForDummies.round(3.1415926, 2) sẽ trả lại giá trị là 3.14. OK?
public class MathForDummies
{
public static double round(double number, int digit)
{
if (digit > 0)
{
int temp = 1, i;
for (i = 0; i < digit; i++)
temp = temp*10;
number = number*temp;
number = Math.round(number);
number = number/temp;
return number;
}
else
return 0.0;
}
}
Oi troi oi, y minh hoi la trong Java co san ham lam tron so hay khong, chu neu tu minh viet ra thi noi lam gi nua. Nhung du sao cung cam on ban da nhiet tinh viet code mau.
Thì mình đã trả lời là có method round như thế rồi, mà bạn lại yêu cầu loại hàm cho nhập cả số chữ số làm tròn đến bao nhiêu nữa --> mình chỉ thấy hơi buồn cười thôi ^_^ Những người xây dựng Java thiết kế class Math như vậy là rất cơ bản và logic rồi. Từ những nguyên liệu cơ bản ấy người lập trình phải tự pt lên theo yêu cầu sự dụng riêng thôi. (vì hàm round đì cùng bộ với ceil và floor mà, ceil và floor đều là hàm trả giá trị nguyên, nếu round cho phép làm tròn theo kiểu bạn yêu cầu thì sẽ mất đi tính hệ thống).
Thân
Mình thấy code của bạn chạy chưa chuẩn lắm. Mình đã sửa cho nó chạy đúng các cậu tham khảo nhé.
Phải test tất cả các trường hợp đặc biệt như 9.097, 10.145, 34.005, 56.015 nữa nhé.
Tớ đã test thử đều chạy đúng cả mọi người test lại hộ tớ với.
public class NumberUtils {
/**
* Method to round decimal number with the corresponding digit
* @param number is rounded
* @param digit
* @return decimal number with amount of numeral after the point is digit
*/
public static double roundNumber(double number, int digit) {
double result = 0.0d;
if (digit > 0) {
String strNumber = String.valueOf(number);
int indexOfPoint = strNumber.indexOf('.');
//split number to 2 part: number before point and number after before point
String strNumberBeforePoint = strNumber.substring(0, indexOfPoint);
String strNumberAfterPoint = strNumber.substring(indexOfPoint + 1, strNumber.length());
String strResultNAP = null;
//case 1: process with two zero after point
if (strNumberAfterPoint.lastIndexOf("0") == 1) {
int third = Integer.parseInt(String.valueOf(strNumberAfterPoin t.charAt(2)));
if (third >= 5) {
strResultNAP = "01";
} else {
strResultNAP = "00";
}
//case 2: process with one zero after point
} else if (strNumberAfterPoint.lastIndexOf("0") == 0) {
int third = Integer.parseInt(String.valueOf(strNumberAfterPoin t.charAt(2)));
int second = Integer.parseInt(String.valueOf(strNumberAfterPoin t.charAt(1)));
if((second == 9)&&(third >= 5)){
strResultNAP = "10";
}else if(third >= 5){
strResultNAP = "0" + String.valueOf(second + 1);
}else{
strResultNAP = "0" + String.valueOf(second + 1);
}
//case 3: process with not zero after point
} else {
int numberAfterPoint = Integer.parseInt(strNumberAfterPoint);
int divide_multi_number = 1;
for (int i = 0; i < digit; i++) {
divide_multi_number = divide_multi_number * 10;
}
double temp = (double) numberAfterPoint / (double) divide_multi_number;
long resultNAP = Math.round(temp);
strResultNAP = String.valueOf(resultNAP);
}
String strResult = strNumberBeforePoint + "." + strResultNAP;
result = new Double(strResult).doubleValue();
}
return result;
}
public static void main(String[] args) {
System.out.println("" + NumberUtils.roundNumber(9.098, 2));
System.out.println("005".charAt(2));
}
}
post lên code nó không được thẳng hàng các cậu copy vào netbeans, eclipse hay myeclipse etc... chỉnh lại hộ mình.
thanks bac nhiều 20202020202
phuc tap qua.Doc code ma cung ko hieu lun.
Bạn thử dùng NumberFormat xem sao
Bookmarks