[Note] Handling of Java decimal point

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)

Handling of decimal point

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

When capturing the numerical value to be handled as a character string

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.

For integer 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

For string type

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.

Digression

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

[Note] Handling of Java decimal point
Handling of java floating point [Note] while reading the reference book
[Java] Practice of exception handling [Exception]
Handling of time zones using Java
Count the number of digits after the decimal point in Java
Step-by-step understanding of Java exception handling
Basic knowledge of Java development Note writing
Java exception handling?
Java Big Decimal
[Java] Exception handling
☾ Java / Exception handling
Java exception handling
Java exception handling
[Java] Overview of Java
[Java] Handling of JavaBeans in the method chain
[Note] Java: Measures the speed of string concatenation
[Note] Java: Speed of List processing by purpose
Expired collection of java
Java abstract modifier [Note]
[Java] Internal Iterator Note
[Java] Significance of serialVersionUID
Please note the division (division) of java kotlin Int and Int
NIO.2 review of java
Review of java Shilber
java --Unification of comments
Java JUnit brief note
[java] Java SE 8 Silver Note
History of Java annotation
[Note] Java: String search
[Note] Java: String survey
java (merits of polymorphism)
My Study Note (Java)
NIO review of java
[Java] Three features of Java
Summary of Java support 2018
[Java] Handling of character strings (String class and StringBuilder class)
java: Add date [Note]
[java] Decimal number → n-ary number n-ary number → decimal number
[Note] Java Output of the sum of odd and even elements
About the default behavior of decimal point rounding using java.text.NumberFormat
[Introduction to Java] Handling of character strings (String class, StringBuilder class)
A note about Java GC
About the handling of Null
Confront Java Floating Point Error
About an instance of java
Handling of SNMP traps (CentOS 8)
[Java] Mirage-Basic usage of SQL
[Java] Beginner's understanding of Servlet-②
[Java11] Stream Summary -Advantages of Stream-
Basics of character operation (java)
(Note) Java classes / variables / methods
[Java] Creation of original annotation
4th day of java learning
Java end of month plusMonths
[Java] About try-catch exception handling
[Java] Summary of regular expressions
[Java] Summary of operators (operator)
[Java] Implementation of Faistel Network
[Java] Comparator of Collection class
[Java] Use Big Decimal properly ~ 2018 ~
Summary of Java language basics