Java Version
java -version
java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)
In Java, rounding up, down, and rounding of integer values is a little difficult to understand. (When exchanging data with integer type <-> floating point type, it tends to be confusing sensuously), so I feel that it is safer to handle numerical values with character strings, so I will leave it as a memorandum as a countermeasure.
I want to round the second decimal place of the original number and then return it to an integer value. EX1) 120->1.20 ---->ROUND_UP 1.20 ->120 ---->ROUND_DOWN 1.20 ->120 ---->ROUND_HALF_UP 1.20 ->120
EX2) 125->1.25 ---->ROUND_UP 1.30 ->130 ---->ROUND_DOWN 1.20 ->120 ---->ROUND_HALF_UP 1.30 ->130
When handling with java.math.BigDecimal, it is necessary to adjust the number of effective digits for the numerical value to be handled once in order to round up, round down, round off, etc. within the range after the decimal point.
foo.java
import java.math.BigDecimal;
public class TestCalc {
public static void main(String[] args) {
/* TestData */
Integer val_120 = 120;
Integer val_125 = 125;
Integer digit = 100; // 10^n
BigDecimal bigDval_120 = new BigDecimal(val_120 / digit.doubleValue());
BigDecimal bigDval_125 = new BigDecimal(val_125 / digit.doubleValue());
//Decimal point n+Round the first digit
int n = 1;
BigDecimal retRoundUp_120 = bigDval_120.setScale(n, BigDecimal.ROUND_UP);
BigDecimal retRoundDown_120 = bigDval_120.setScale(n, BigDecimal.ROUND_DOWN);
BigDecimal retRoundHalfUp_120 = bigDval_120.setScale(n, BigDecimal.ROUND_HALF_UP);
BigDecimal retRoundUp_125 = bigDval_125.setScale(n, BigDecimal.ROUND_UP);
BigDecimal retRoundDown_125 = bigDval_125.setScale(n, BigDecimal.ROUND_DOWN);
BigDecimal retRoundHalfUp_125 = bigDval_125.setScale(n, BigDecimal.ROUND_HALF_UP);
//Round up / round down / round
System.out.println(retRoundUp_120 + "<--" + bigDval_120);
System.out.println(retRoundDown_120 + "<--" + bigDval_120);
System.out.println(retRoundHalfUp_120 + "<--" + bigDval_120);
System.out.println(retRoundUp_125 + "<--" + bigDval_125);
System.out.println(retRoundDown_125 + "<--" + bigDval_125);
System.out.println(retRoundHalfUp_125 + "<--" + bigDval_125);
//Convert to int
System.out.println(retRoundUp_120.intValue() + "<--" + retRoundUp_120);
System.out.println(retRoundDown_120.intValue() + "<--" + retRoundDown_120);
System.out.println(retRoundHalfUp_120.intValue() + "<--" + retRoundHalfUp_120);
System.out.println(retRoundUp_125.intValue() + "<--" + retRoundUp_125);
System.out.println(retRoundDown_125.intValue() + "<--" + retRoundDown_125);
System.out.println(retRoundHalfUp_125.intValue() + "<--" + retRoundHalfUp_125);
}
}
Execution result
1.2<--1.1999999999999999555910790149937383830547332763671875
1.1<--1.1999999999999999555910790149937383830547332763671875
1.2<--1.1999999999999999555910790149937383830547332763671875
1.3<--1.25
1.2<--1.25
1.3<--1.25
1<--1.2
1<--1.1
1<--1.2
1<--1.3
1<--1.2
1<--1.3
foo.java
import java.math.BigDecimal;
public class TestCalc {
public static void main(String[] args) {
/* TestData */
BigDecimal bigDval_120 = new BigDecimal("1.20");
BigDecimal bigDval_125 = new BigDecimal("1.25");
//Decimal point n+Round the first digit
int n = 1;
BigDecimal retRoundUp_120 = bigDval_120.setScale(n, BigDecimal.ROUND_UP);
BigDecimal retRoundDown_120 = bigDval_120.setScale(n, BigDecimal.ROUND_DOWN);
BigDecimal retRoundHalfUp_120 = bigDval_120.setScale(n, BigDecimal.ROUND_HALF_UP);
BigDecimal retRoundUp_125 = bigDval_125.setScale(n, BigDecimal.ROUND_UP);
BigDecimal retRoundDown_125 = bigDval_125.setScale(n, BigDecimal.ROUND_DOWN);
BigDecimal retRoundHalfUp_125 = bigDval_125.setScale(n, BigDecimal.ROUND_HALF_UP);
//Round up / round down / round
System.out.println(retRoundUp_120 + "<--" + bigDval_120);
System.out.println(retRoundDown_120 + "<--" + bigDval_120);
System.out.println(retRoundHalfUp_120 + "<--" + bigDval_120);
System.out.println(retRoundUp_125 + "<--" + bigDval_125);
System.out.println(retRoundDown_125 + "<--" + bigDval_125);
System.out.println(retRoundHalfUp_125 + "<--" + bigDval_125);
//Convert to int
System.out.println(retRoundUp_120.intValue() + "<--" + retRoundUp_120);
System.out.println(retRoundDown_120.intValue() + "<--" + retRoundDown_120);
System.out.println(retRoundHalfUp_120.intValue() + "<--" + retRoundHalfUp_120);
System.out.println(retRoundUp_125.intValue() + "<--" + retRoundUp_125);
System.out.println(retRoundDown_125.intValue() + "<--" + retRoundDown_125);
System.out.println(retRoundHalfUp_125.intValue() + "<--" + retRoundHalfUp_125);
}
}
Execution result
1.2<--1.20
1.2<--1.20
1.2<--1.20
1.3<--1.25
1.2<--1.25
1.3<--1.25
1<--1.2
1<--1.2
1<--1.2
1<--1.3
1<--1.2
1<--1.3
From the above, BigDecimal requires child care when converting to a floating point integer value, regardless of whether it is handled as a floating point type or a character string type.
foo.java
import java.math.BigDecimal;
public class TestCalc {
public static void main(String[] args) {
/* TestData */
int val_120 = 120;
int val_125 = 125;
Integer digit = 100; // 10^(n+1)
BigDecimal bigDval_120 = new BigDecimal(val_120 / digit.doubleValue());
BigDecimal bigDval_125 = new BigDecimal(val_125 / digit.doubleValue());
//Decimal point n+Round the first digit
int n = 1;
BigDecimal retRoundUp_120 = bigDval_120.setScale(n, BigDecimal.ROUND_UP);
BigDecimal retRoundDown_120 = bigDval_120.setScale(n, BigDecimal.ROUND_DOWN);
BigDecimal retRoundHalfUp_120 = bigDval_120.setScale(n, BigDecimal.ROUND_HALF_UP);
BigDecimal retRoundUp_125 = bigDval_125.setScale(n, BigDecimal.ROUND_UP);
BigDecimal retRoundDown_125 = bigDval_125.setScale(n, BigDecimal.ROUND_DOWN);
BigDecimal retRoundHalfUp_125 = bigDval_125.setScale(n, BigDecimal.ROUND_HALF_UP);
//Round up / round down / round
System.out.println(retRoundUp_120 + "<--" + bigDval_120);
System.out.println(retRoundDown_120 + "<--" + bigDval_120);
System.out.println(retRoundHalfUp_120 + "<--" + bigDval_120);
System.out.println(retRoundUp_125 + "<--" + bigDval_125);
System.out.println(retRoundDown_125 + "<--" + bigDval_125);
System.out.println(retRoundHalfUp_125 + "<--" + bigDval_125);
//Raise digits before converting to int
System.out.println(Double.valueOf(retRoundUp_120.doubleValue() * digit).intValue()+"<--"+retRoundUp_120.doubleValue() * digit + "<--" + retRoundUp_120);
System.out.println(Double.valueOf(retRoundDown_120.doubleValue() * digit).intValue()+"<--"+retRoundDown_120.doubleValue() * digit + "<--" +retRoundDown_120);
System.out.println(Double.valueOf(retRoundHalfUp_120.doubleValue() * digit).intValue()+"<--"+retRoundHalfUp_120.doubleValue() * digit + "<--" +retRoundHalfUp_120);
System.out.println(Double.valueOf(retRoundUp_125.doubleValue() * digit).intValue()+"<--"+retRoundUp_125.doubleValue() * digit + "<--" +retRoundUp_125);
System.out.println(Double.valueOf(retRoundDown_125.doubleValue() * digit).intValue()+"<--"+retRoundDown_125.doubleValue() * digit + "<--" +retRoundDown_125);
System.out.println(Double.valueOf(retRoundHalfUp_125.doubleValue() * digit).intValue()+"<--"+retRoundHalfUp_125.doubleValue() * digit + "<--" +retRoundHalfUp_125);
}
}
Execution result
1.2<--1.1999999999999999555910790149937383830547332763671875
1.1<--1.1999999999999999555910790149937383830547332763671875
1.2<--1.1999999999999999555910790149937383830547332763671875
1.3<--1.25
1.2<--1.25
1.3<--1.25
120<--120.0<--1.2
110<--110.00000000000001<--1.1
120<--120.0<--1.2
130<--130.0<--1.3
120<--120.0<--1.2
130<--130.0<--1.3
import java.math.BigDecimal;
public class TestCalc {
public static void main(String[] args) {
/* TestData */
Integer digit = 100; // 10^(n+1)
BigDecimal bigDval_120 = new BigDecimal("1.20");
BigDecimal bigDval_125 = new BigDecimal("1.25");
//Decimal point n+Round the first digit
int n = 1;
BigDecimal retRoundUp_120 = bigDval_120.setScale(n, BigDecimal.ROUND_UP);
BigDecimal retRoundDown_120 = bigDval_120.setScale(n, BigDecimal.ROUND_DOWN);
BigDecimal retRoundHalfUp_120 = bigDval_120.setScale(n, BigDecimal.ROUND_HALF_UP);
BigDecimal retRoundUp_125 = bigDval_125.setScale(n, BigDecimal.ROUND_UP);
BigDecimal retRoundDown_125 = bigDval_125.setScale(n, BigDecimal.ROUND_DOWN);
BigDecimal retRoundHalfUp_125 = bigDval_125.setScale(n, BigDecimal.ROUND_HALF_UP);
//Round up / round down / round
System.out.println(retRoundUp_120 + "<--" + bigDval_120);
System.out.println(retRoundDown_120 + "<--" + bigDval_120);
System.out.println(retRoundHalfUp_120 + "<--" + bigDval_120);
System.out.println(retRoundUp_125 + "<--" + bigDval_125);
System.out.println(retRoundDown_125 + "<--" + bigDval_125);
System.out.println(retRoundHalfUp_125 + "<--" + bigDval_125);
//Raise digits before converting to int
System.out.println(Double.valueOf(retRoundUp_120.doubleValue() * digit).intValue()+"<--"+retRoundUp_120.doubleValue() * digit + "<--" + retRoundUp_120);
System.out.println(Double.valueOf(retRoundDown_120.doubleValue() * digit).intValue()+"<--"+retRoundDown_120.doubleValue() * digit + "<--" +retRoundDown_120);
System.out.println(Double.valueOf(retRoundHalfUp_120.doubleValue() * digit).intValue()+"<--"+retRoundHalfUp_120.doubleValue() * digit + "<--" +retRoundHalfUp_120);
System.out.println(Double.valueOf(retRoundUp_125.doubleValue() * digit).intValue()+"<--"+retRoundUp_125.doubleValue() * digit + "<--" +retRoundUp_125);
System.out.println(Double.valueOf(retRoundDown_125.doubleValue() * digit).intValue()+"<--"+retRoundDown_125.doubleValue() * digit + "<--" +retRoundDown_125);
System.out.println(Double.valueOf(retRoundHalfUp_125.doubleValue() * digit).intValue()+"<--"+retRoundHalfUp_125.doubleValue() * digit + "<--" +retRoundHalfUp_125);
}
}
Execution result
1.2<--1.20
1.2<--1.20
1.2<--1.20
1.3<--1.25
1.2<--1.25
1.3<--1.25
120<--120.0<--1.2
120<--120.0<--1.2
120<--120.0<--1.2
130<--130.0<--1.3
120<--120.0<--1.2
130<--130.0<--1.3
It seems that it is better to adjust the number of effective digits of the decimal point with the character string type, but in the end it has to be the integer type, so I will try to find a better method.
If you just want to round, you can use Math.round, but if you want to use rounding up or down, you'll probably use Big Decimal.
test.java
double d1 = 0.50;
double d2 = 0.25;
System.out.println(Math.round(d1)+":"+Math.round(d2));
Execution result
1:0
Recommended Posts