Notice
Recent Posts
Recent Comments
Link
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Archives
Today
Total
관리 메뉴

Everything has an expiration date

133 - Java BigDecimal Class : 범위가 매우 큰 소수를 표현하는 클래스. movePointLeft(), divide(), BigDecimal.ROUND_DOWN 본문

[Java]/Program source (java)

133 - Java BigDecimal Class : 범위가 매우 큰 소수를 표현하는 클래스. movePointLeft(), divide(), BigDecimal.ROUND_DOWN

Jelly-fish 2023. 9. 15. 17:51
/*==============================================
  ■■■ 자바의 주요(중요) 클래스 ■■■
  - 자바에서 기본적으로 제공하는 주요 클래스들
  - BigDecimal 클래스
================================================*/

import java.math.BigDecimal;


public class Test133
{
	public static void main(String[] args)
	{
		BigDecimal a = new BigDecimal("123456789.123456789");

		// movePointLeft() : 이동시킨다, 포인트(소수점), 왼편으로
		// 매개변수 숫자만큼 소수점을 왼쪽으로 이동시키는 메소드

		// movePointLeft() : 소수점을 왼쪽으로 이동
		BigDecimal b = a.movePointLeft(3);
		System.out.println("처리 결과 : " + b);
		//--==>> 처리 결과 : 123456.789123456789

		// 나눗셈 연산
		// a (123456789.123456789) 를 b (123456.789123456789) 로 나눈 결과를 처리.
		BigDecimal c = a.divide(b);
		System.out.println("처리 결과 : " + c);
		//--==>> 처리 결과 : 1E+3

		// static final 변수 : ROUND_DOWN
		// API
		/*
		public static final int ROUND_DOWN
		Rounding mode to round towards zero.
		Never increments the digit prior to a discarded fraction (i.e., truncates).
		Note that this rounding mode never increases the magnitude of the calculated value.
		*/

		BigDecimal d = a.divide(b, BigDecimal.ROUND_DOWN); // 반올림을 하지 않는다. → 절삭
		System.out.println("처리 결과 : " + d);
		//--==>> 처리 결과 : 1000.000000000


		// BigInteger 객체로 변환
		System.out.println(a.toBigInteger());
		//--==>> 123456789



	}
}


// 실행 결과

/*
처리 결과 : 123456.789123456789
처리 결과 : 1E+3
처리 결과 : 1000.000000000
123456789
계속하려면 아무 키나 누르십시오 . . .
*/