Javaは整数をとる
6542 ワード
String arch = System.getProperty("sun.arch.data.model");
小数点以下の整数を切り捨てる:Math.floor(2.0)=2
小数点以下の整数を切り捨てる:Math.floor(2.1)=2
小数点以下を切り捨てる:Math.floor(2.5)=2
小数点以下を切り捨てる:Math.floor(2.9)=2
小数点以下を切り捨てる:Math.floor(-2.0)=-2
小数点以下を切り捨てる:Math.floor(-2.1)=-3
小数点以下を切り捨てる:Math.floor(-2.5)=-3
小数点以下を切り捨てる:Math.floor(-2.9)=-3
四捨五入:Math.rint(2.0)=2
四捨五入:Math.rint(2.1)=2
四捨五入:Math.rint(2.5)=2
四捨五入:Math.rint(2.9)=3
四捨五入:Math.rint(-2.0)=-2
四捨五入:Math.rint(-2.1)=-2
四捨五入:Math.rint(-2.5)=-2
四捨五入:Math.rint(-2.9)=-3
小数点以下の整数を切り捨てる:Math.floor(2.0)=2
小数点以下の整数を切り捨てる:Math.floor(2.1)=2
小数点以下を切り捨てる:Math.floor(2.5)=2
小数点以下を切り捨てる:Math.floor(2.9)=2
小数点以下を切り捨てる:Math.floor(-2.0)=-2
小数点以下を切り捨てる:Math.floor(-2.1)=-3
小数点以下を切り捨てる:Math.floor(-2.5)=-3
小数点以下を切り捨てる:Math.floor(-2.9)=-3
四捨五入:Math.rint(2.0)=2
四捨五入:Math.rint(2.1)=2
四捨五入:Math.rint(2.5)=2
四捨五入:Math.rint(2.9)=3
四捨五入:Math.rint(-2.0)=-2
四捨五入:Math.rint(-2.1)=-2
四捨五入:Math.rint(-2.5)=-2
四捨五入:Math.rint(-2.9)=-3
import java.math.BigDecimal;
import java.text.DecimalFormat;
public class TestGetInt {
public static void main(String[] args) {
double i = 2, j = 2.1, k = 2.5, m = 2.9;
System.out.println(" :Math.floor(" + i + ")=" + (int) Math.floor(i));
System.out.println(" :Math.floor(" + j + ")=" + (int) Math.floor(j));
System.out.println(" :Math.floor(" + k + ")=" + (int) Math.floor(k));
System.out.println(" :Math.floor(" + m + ")=" + (int) Math.floor(m));
System.out.println();
System.out.println(" :Math.floor(-" + i + ")=" + (int) Math.floor(-i));
System.out.println(" :Math.floor(-" + j + ")=" + (int) Math.floor(-j));
System.out.println(" :Math.floor(-" + k + ")=" + (int) Math.floor(-k));
System.out.println(" :Math.floor(-" + m + ")=" + (int) Math.floor(-m));
System.out.println();
//
System.out.println(" :Math.rint(" + i + ")=" + (int) Math.rint(i));
System.out.println(" :Math.rint(" + j + ")=" + (int) Math.rint(j));
System.out.println(" :Math.rint(" + k + ")=" + (int) Math.rint(k));
System.out.println(" :Math.rint(" + m + ")=" + (int) Math.rint(m));
System.out.println();
System.out.println(" :Math.rint(-" + i + ")=" + (int) Math.rint(-i));
System.out.println(" :Math.rint(-" + j + ")=" + (int) Math.rint(-j));
System.out.println(" :Math.rint(-" + k + ")=" + (int) Math.rint(-k));
System.out.println(" :Math.rint(-" + m + ")=" + (int) Math.rint(-m));
System.out.println();
System.out.println("DecimalFormat :(" + i + ")="
+ new DecimalFormat("0").format(i));
System.out.println("DecimalFormat :(" + j + ")="
+ new DecimalFormat("0").format(j));
System.out.println("DecimalFormat :(" + k + ")="
+ new DecimalFormat("0").format(k));
System.out.println("DecimalFormat :(" + m + ")="
+ new DecimalFormat("0").format(m));
System.out.println();
System.out.println("DecimalFormat :(-" + i + ")="
+ new DecimalFormat("0").format(-i));
System.out.println("DecimalFormat :(-" + j + ")="
+ new DecimalFormat("0").format(-j));
System.out.println("DecimalFormat :(-" + k + ")="
+ new DecimalFormat("0").format(-k));
System.out.println("DecimalFormat :(-" + m + ")="
+ new DecimalFormat("0").format(-m));
System.out.println();
System.out.println("BigDecimal :(" + i + ")="
+ new BigDecimal("2").setScale(0, BigDecimal.ROUND_HALF_UP));
System.out.println("BigDecimal :(" + j + ")="
+ new BigDecimal("2.1").setScale(0, BigDecimal.ROUND_HALF_UP));
System.out.println("BigDecimal :(" + k + ")="
+ new BigDecimal("2.5").setScale(0, BigDecimal.ROUND_HALF_UP));
System.out.println("BigDecimal :(" + m + ")="
+ new BigDecimal("2.9").setScale(0, BigDecimal.ROUND_HALF_UP));
System.out.println();
System.out.println("BigDecimal :(-" + i + ")="
+ new BigDecimal("-2").setScale(0, BigDecimal.ROUND_HALF_UP));
System.out.println("BigDecimal :(-" + j + ")="
+ new BigDecimal("-2.1").setScale(0, BigDecimal.ROUND_HALF_UP));
System.out.println("BigDecimal :(-" + k + ")="
+ new BigDecimal("-2.5").setScale(0, BigDecimal.ROUND_HALF_UP));
System.out.println("BigDecimal :(-" + m + ")="
+ new BigDecimal("-2.9").setScale(0, BigDecimal.ROUND_HALF_UP));
System.out.println();
System.out.println(" :Math.ceil(" + i + ")=" + (int) Math.ceil(i));
System.out.println(" :Math.ceil(" + j + ")=" + (int) Math.ceil(j));
System.out.println(" :Math.ceil(" + k + ")=" + (int) Math.ceil(k));
System.out.println(" :Math.ceil(" + m + ")=" + (int) Math.ceil(m));
System.out.println();
System.out.println(" :Math.ceil(-" + i + ")=" + (int) Math.ceil(-i));
System.out.println(" :Math.ceil(-" + j + ")=" + (int) Math.ceil(-j));
System.out.println(" :Math.ceil(-" + k + ")=" + (int) Math.ceil(-k));
System.out.println(" :Math.ceil(-" + m + ")=" + (int) Math.ceil(-m));
System.out.println();
System.out.println(" :Math.round(" + i + ")=" + (int) Math.round(i));
System.out.println(" :Math.round(" + j + ")=" + (int) Math.round(j));
System.out.println(" :Math.round(" + k + ")=" + (int) Math.round(k));
System.out.println(" :Math.round(" + m + ")=" + (int) Math.round(m));
System.out.println();
System.out.println(" :Math.round(-" + i + ")=" + (int) Math.round(-i));
System.out.println(" :Math.round(-" + j + ")=" + (int) Math.round(-j));
System.out.println(" :Math.round(-" + k + ")=" + (int) Math.round(-k));
System.out.println(" :Math.round(-" + m + ")=" + (int) Math.round(-m));
}
}