Bài viết này sẽ hướng dẫn cách chuyển đổi kiểu dữ liệu DOUBLE thành INT làm tròn số trong ngôn ngữ lập trình java.
ConvertDoubleToIntRound.java
/**
* @(#)ConvertDoubleToIntRound.java
*
* ConvertDoubleToIntRound application
*
* @author BUI NGOC SON
* @version 1.00 2013/12/5
*/
public class ConvertDoubleToIntRound {
public static void main(String[] args) {
// TODO, add your application code
int variable_01 = (int) Math.round(4.99999); //gives 5 only
int variable_02 = (int) Math.round(4.11111); //gives 4 only
System.out.println("Variable 01: " + variable_01);
System.out.println("Variable 02: " + variable_02);
System.out.println("\n \n VNLIVES.NET");
}
}
* @(#)ConvertDoubleToIntRound.java
*
* ConvertDoubleToIntRound application
*
* @author BUI NGOC SON
* @version 1.00 2013/12/5
*/
public class ConvertDoubleToIntRound {
public static void main(String[] args) {
// TODO, add your application code
int variable_01 = (int) Math.round(4.99999); //gives 5 only
int variable_02 = (int) Math.round(4.11111); //gives 4 only
System.out.println("Variable 01: " + variable_01);
System.out.println("Variable 02: " + variable_02);
System.out.println("\n \n VNLIVES.NET");
}
}
Dưới đây là kết quả sau khi chạy chương trình.
No comments:
Post a Comment