四捨五入
| 日本語 | 四捨五入 |
| 英語 | round |
| ふりがな | ししゃごにゅう |
| フリガナ | シシャゴニュウ |
小数点以下での丸め方のひとつ。
小数点以下の特定の桁より下を消去する(0にする)場合の方法のひとつ。その「特定の桁」が0~4の場合には「そのまま切り捨て」、5~9の場合には「特定の桁」の左の桁に1を加える方法。
BigDecimalクラスのsetScale()メソッドで切り上げ、切り捨て、四捨五入を行うことができ、このメソッドの第1引数に「四捨五入を行う桁(上記の例だと「特定の桁」の右の桁)」、第2引数にBigDecimalクラスのROUND_HALF_UPフィールドを渡すことで四捨五入をすることができる。
また、double型の場合にはround()メソッド及びrint()メソッドで四捨五入ができる。
小数点以下の特定の桁より下を消去する(0にする)場合の方法のひとつ。その「特定の桁」が0~4の場合には「そのまま切り捨て」、5~9の場合には「特定の桁」の左の桁に1を加える方法。
BigDecimalクラスのsetScale()メソッドで切り上げ、切り捨て、四捨五入を行うことができ、このメソッドの第1引数に「四捨五入を行う桁(上記の例だと「特定の桁」の右の桁)」、第2引数にBigDecimalクラスのROUND_HALF_UPフィールドを渡すことで四捨五入をすることができる。
また、double型の場合にはround()メソッド及びrint()メソッドで四捨五入ができる。
// Sample.java
import java.math.BigDecimal;
import java.text.DecimalFormat;
public class Sample
{
public static void main( String[] args )
{
// 出力フォーマットを用意しておきます。
String format = "0.00000000000000000000";
DecimalFormat decimalFormat = new DecimalFormat( format );
BigDecimal bigDecimal = new BigDecimal( "4.55543" );
System.out.println( decimalFormat.format( bigDecimal ) );
// 4.55543000000000000000
// 小数点以下2位の数に四捨五入します。
// BigDecimal.ROUND_HALF_UPが四捨五入用フラグです。
System.out.println( decimalFormat.format( bigDecimal.setScale( 2, BigDecimal.ROUND_HALF_UP ) ) );
// 4.56000000000000000000
System.out.println( decimalFormat.format( bigDecimal.setScale( 3, BigDecimal.ROUND_HALF_UP ) ) );
// 4.55500000000000000000
// double型ならMathクラスのround()メソッドかrint()メソッドを使用します。
System.out.println( Math.round( 1.0 ) );
System.out.println( Math.round( 1.1 ) );
System.out.println( Math.round( 1.4 ) );
System.out.println( Math.round( 1.5 ) );
System.out.println( Math.round( 1.9 ) );
System.out.println( Math.round( 2.0 ) );
// 1
// 1
// 1
// 2
// 2
// 2
System.out.println( Math.rint( 1.0 ) );
System.out.println( Math.rint( 1.1 ) );
System.out.println( Math.rint( 1.4 ) );
System.out.println( Math.rint( 1.5 ) );
System.out.println( Math.rint( 1.9 ) );
System.out.println( Math.rint( 2.0 ) );
// 1.0
// 1.0
// 1.0
// 2.0
// 2.0
// 2.0
}
}
import java.math.BigDecimal;
import java.text.DecimalFormat;
public class Sample
{
public static void main( String[] args )
{
// 出力フォーマットを用意しておきます。
String format = "0.00000000000000000000";
DecimalFormat decimalFormat = new DecimalFormat( format );
BigDecimal bigDecimal = new BigDecimal( "4.55543" );
System.out.println( decimalFormat.format( bigDecimal ) );
// 4.55543000000000000000
// 小数点以下2位の数に四捨五入します。
// BigDecimal.ROUND_HALF_UPが四捨五入用フラグです。
System.out.println( decimalFormat.format( bigDecimal.setScale( 2, BigDecimal.ROUND_HALF_UP ) ) );
// 4.56000000000000000000
System.out.println( decimalFormat.format( bigDecimal.setScale( 3, BigDecimal.ROUND_HALF_UP ) ) );
// 4.55500000000000000000
// double型ならMathクラスのround()メソッドかrint()メソッドを使用します。
System.out.println( Math.round( 1.0 ) );
System.out.println( Math.round( 1.1 ) );
System.out.println( Math.round( 1.4 ) );
System.out.println( Math.round( 1.5 ) );
System.out.println( Math.round( 1.9 ) );
System.out.println( Math.round( 2.0 ) );
// 1
// 1
// 1
// 2
// 2
// 2
System.out.println( Math.rint( 1.0 ) );
System.out.println( Math.rint( 1.1 ) );
System.out.println( Math.rint( 1.4 ) );
System.out.println( Math.rint( 1.5 ) );
System.out.println( Math.rint( 1.9 ) );
System.out.println( Math.rint( 2.0 ) );
// 1.0
// 1.0
// 1.0
// 2.0
// 2.0
// 2.0
}
}
// Sample.java
import java.math.BigDecimal;
import java.text.DecimalFormat;
public class Sample
{
public static void main( String[] args )
{
// 出力フォーマットを用意しておきます。
String format = "0.00000000000000000000";
DecimalFormat decimalFormat = new DecimalFormat( format );
BigDecimal bigDecimal = new BigDecimal( "4.55543" );
System.out.println( decimalFormat.format( bigDecimal ) );
// 4.55543000000000000000
// 小数点以下2位の数に四捨五入します。
// BigDecimal.ROUND_HALF_UPが四捨五入用フラグです。
System.out.println( decimalFormat.format( bigDecimal.setScale( 2, BigDecimal.ROUND_HALF_UP ) ) );
// 4.56000000000000000000
System.out.println( decimalFormat.format( bigDecimal.setScale( 3, BigDecimal.ROUND_HALF_UP ) ) );
// 4.55500000000000000000
// double型ならMathクラスのround()メソッドかrint()メソッドを使用します。
System.out.println( Math.round( 1.0 ) );
System.out.println( Math.round( 1.1 ) );
System.out.println( Math.round( 1.4 ) );
System.out.println( Math.round( 1.5 ) );
System.out.println( Math.round( 1.9 ) );
System.out.println( Math.round( 2.0 ) );
// 1
// 1
// 1
// 2
// 2
// 2
System.out.println( Math.rint( 1.0 ) );
System.out.println( Math.rint( 1.1 ) );
System.out.println( Math.rint( 1.4 ) );
System.out.println( Math.rint( 1.5 ) );
System.out.println( Math.rint( 1.9 ) );
System.out.println( Math.rint( 2.0 ) );
// 1.0
// 1.0
// 1.0
// 2.0
// 2.0
// 2.0
}
}




