Java BigDecimal Division Error: "Non-terminating decimal expansion; no exact representable decimal result"
The error message "Non-terminating decimal expansion; no exact representable decimal result" means that the division operation resulted in a decimal number with an infinite decimal expansion that cannot be represented exactly.\n\nTo fix this issue, you can use the divide method with a specified scale and rounding mode to control the precision of the decimal result. For example, you can modify the division calculations as follows:\n\njava\nBigDecimal fiveWorkdayRate =\n (fiveWorkday == 0) ?\n BigDecimal.ZERO :\n new BigDecimal(fiveWorkday).divide(new BigDecimal(all), 2, RoundingMode.HALF_UP);\nBigDecimal tenWorkdayRate =\n (tenWorkday == 0) ?\n BigDecimal.ZERO :\n new BigDecimal(tenWorkday).divide(new BigDecimal(all), 2, RoundingMode.HALF_UP);\nBigDecimal fifteenWorkdayRate =\n (fifteenWorkday == 0) ?\n BigDecimal.ZERO :\n new BigDecimal(fifteenWorkday).divide(new BigDecimal(all), 2, RoundingMode.HALF_UP);\nBigDecimal twentyWorkdayRate =\n (twentyWorkday == 0) ?\n BigDecimal.ZERO :\n new BigDecimal(twentyWorkday).divide(new BigDecimal(all), 2, RoundingMode.HALF_UP);\n\n\nBy specifying a scale of 2 and using the RoundingMode.HALF_UP rounding mode, the result will be rounded to two decimal places. If you prefer a different rounding mode, you can choose from the available options provided by the RoundingMode enum.
原文地址: https://www.cveoy.top/t/topic/mFH5 著作权归作者所有。请勿转载和采集!