package
TestTestBufferedOutputStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.math.RoundingMode;
public class NumberFormat ... {
public static void main(String[] args) ...{
BigDecimal a;
BigDecimal b;
BigDecimal divA = BigDecimal.valueOf(126000);
BigDecimal divB = new BigDecimal(10);
a = divA.divide(divB, 0, RoundingMode.HALF_UP);
b = divA.divide(divB, 2, RoundingMode.HALF_UP);
Long money1 = Long.valueOf(a.longValue());
Long money2 = Long.valueOf(b.longValue());
System.out.println("四捨五入: " + divA + " ÷ " + divB + " = " + money1);
System.out.println("小数点第2位で四捨五入: " + divA + " ÷ " + divB + " = " + money2);
// 数字をフォーマットして、出力します
DecimalFormat df=new DecimalFormat();
DecimalFormat df1=(DecimalFormat) DecimalFormat.getInstance();
df.setMaximumFractionDigits(2);
df.applyPattern(".##");
System.out.println(df.format(12.22));
}
}
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.math.RoundingMode;
public class NumberFormat ... {
public static void main(String[] args) ...{
BigDecimal a;
BigDecimal b;
BigDecimal divA = BigDecimal.valueOf(126000);
BigDecimal divB = new BigDecimal(10);
a = divA.divide(divB, 0, RoundingMode.HALF_UP);
b = divA.divide(divB, 2, RoundingMode.HALF_UP);
Long money1 = Long.valueOf(a.longValue());
Long money2 = Long.valueOf(b.longValue());
System.out.println("四捨五入: " + divA + " ÷ " + divB + " = " + money1);
System.out.println("小数点第2位で四捨五入: " + divA + " ÷ " + divB + " = " + money2);
// 数字をフォーマットして、出力します
DecimalFormat df=new DecimalFormat();
DecimalFormat df1=(DecimalFormat) DecimalFormat.getInstance();
df.setMaximumFractionDigits(2);
df.applyPattern(".##");
System.out.println(df.format(12.22));
}
}