Amount calculation and format conversion __java

Source: Internet
Author: User
These two days help Zhang Xreview the code of the Amountformat class, which involves the amount conversion and display of long (minutes), double (meta) data types.     Since there are rules in the findbugs that do not allow the use of float and double data types to calculate the amount, you can say the specification of the amount calculation in the program: 1. All amounts involved in decimal are calculated using BigDecimal.     2. When creating BigDecimal, integers, strings use the BigDecimal (long/int/string) constructor, and decimals use the bigdecimal.valueof (double) method. 3. After converting all participating numbers to BigDecimal, use the functions of the BigDecimal itself (add, subtract, multiply, divide) for calculation. Do not create a BigDecimal object after the calculation (the calculation may have lost precision). Anti-Example: New BigDecimal (fen/100.0).     Correct: New BigDecimal (fen). Divide (new BigDecimal (100)) 4. The Intercept of the decimal places after the final result is obtained, using DecimalFormat to complete. 5. Because the end result is a string, it is not a problem to display. For saving, save after the parse (example: Long.parselong (String)) function is required for the data type reason.
The two main methods of the 1.DecimalFormat format and Parse,format methods are thread safe, while the parse method is not.
2. According to the above conclusion, if you only use the Format method, you can share a DecimalFormat instance with multiple threads.     If you want to use the parse method, you need each thread to create an instance individually. 3. Finally, the methods in the Amountformat tool class can be invoked by multiple threads at the same time, but the methods in the class only take advantage of the format method, so it is possible to create a private DecimalFormat instance in the class to be used by methods in the class.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.