Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use BigDecimal to do Mathematical Operation in java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use BigDecimal to do mathematical operations in java". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use BigDecimal to do mathematical operations in java".

1. When BigDecimal is doing addition, subtraction and multiplication, the precision will not be lost, but when doing division, there is a situation that cannot be divided, so it is necessary to specify the precision and how to truncate.

Import java.math.BigDecimal; import java.math.RoundingMode; public class Demo {public static void main (String [] args) {BigDecimal D1 = new BigDecimal ("123.456"); BigDecimal D2 = new BigDecimal ("23.456789"); BigDecimal D3 = d1.divide (D2, 10, RoundingMode.HALF_UP); / / keep 10 decimal places and round BigDecimal D4 = d1.divide (D2) / / error report: ArithmeticException, because it can not be divided}}

2. You can find the rest while dividing BigDecimal.

Import java.math.BigDecimal; public class Demo {public static void main (String [] args) {BigDecimal n = new BigDecimal ("22.444"); BigDecimal m = new BigDecimal ("0.23"); BigDecimal [] dr = n.divideAndRemainder (m); System.out.println (dr [0]); / / 97.0 System.out.println (dr [1]) / / 0.134}} Thank you for your reading. The above is the content of "how to do mathematical operations with BigDecimal in java". After the study of this article, I believe you have a deeper understanding of how to do mathematical operations with BigDecimal in java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report