Java large 浮点 number

BigDecimal

Reference article from https://blog.csdn.net/GD_ONE/article/details/103951501 and Baidu Encyclopedia

Accuracy problems

BigDecimal rounding mode

ROUND_DOWN rounded to zero. I.e. 1.5 becomes 1.55, becomes -1.5 -1.55

ROUND_UP in a direction away from zero rounding i.e., 1.55 becomes 1.6, becomes -1.6 -1.55

ROUND_CEILING positive infinity rounding direction. That becomes 1.55 1.6 -1.55 becomes -1.5

ROUND_FLOOR infinity rounding to negative. That becomes 1.55 1.5 -1.55 becomes -1.6

ROUND_HALF_UP rounding ie 1.55 becomes 1.6, -1.55 becomes -1.6

ROUND_HALF_DOWN five off and six ie 1.55 becomes 1.5, -1.5 becomes -1.5

ROUND_HALF_EVEN If a number before rounding is even, odd HALF_DOWN is used as is used HALF_UP 1.55 1.45 HALF_UP employed using HALF_DOWN

ROUND_UNNECESSARY when the exact number of digits, no rounding

May be rounded as performing arithmetic BigDecimal

For example division divide(BigDecimal divisor, int scale, RoundingMode roundingMode)returns a BigDecimal, whose value (this / divisor), which is a decimal scale.

import java.math.BigDecimal;

public class Main {

	public static void main(String[] args) {
		BigDecimal d1 = new BigDecimal("3.1415926535");
		BigDecimal d2 = new BigDecimal("2.7182818284");
		BigDecimal d3 = d1.divide(d2, 10, BigDecimal.ROUND_UP);
		System.out.println(3.1415926535 / 2.7182818284);
		System.out.println(d3);
	}
}

Export

1.155727349782993
1.1557273498
发布了32 篇原创文章 · 获赞 18 · 访问量 3237

Guess you like

Origin blog.csdn.net/u011714517/article/details/104114871