In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you how java retains the relevant knowledge points of two decimal places. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
Using BigDecimal classes for data formatting
The BigDecimal class is an API class provided in the java.math package that can be used to perform precise operations on data with more than 16 significant bits. It provides a format () method that can be used to format numeric values. The specific formatting code is as follows:
Public static String format1 (double value) {BigDecimal bd = new BigDecimal (value); / / create a bd object and pass the value value to be converted into the constructor bd = bd.setScale (2, RoundingMode.HALF_UP); / / call the setScale method to format the data, retaining two decimal places, using the rounding rule return bd.toString (); / / return the value of the bd object (converted to string)} to format the data using the DecimalFormat class
The DecimalFormat class is a concrete subclass of NumberFormat and is used to format decimal numbers. Data can also be easily formatted using this method. The specific formatting code is as follows:
Public static String format2 (double value) {DecimalFormat df = new DecimalFormat ("0.00"); / / create a df object. Passing 0.00 indicates the construction of a df object df.setRoundingMode (RoundingMode.HALF_UP) that retains two places after the decimal point; / / sets the rule, which also uses the rounding rule return df.format (value) / / return value (format the data using the formatting method of the df object before returning)} use the NumberFormat class to format the data
NumberFormat is the abstract base class for all digital formats. It provides an interface for formatting and parsing numbers. It can help developers format and parse numbers in any locale. The code to format the data using this method is as follows:
Public static String format3 (double value) {NumberFormat nf = NumberFormat.getNumberInstance (); / / to get an instance of the NumberFormat class, you need to call the getInstance () method nf.setMaximumFractionDigits (2); / / set a mandatory reservation of two bits to avoid returning 10 (that is, two bits to be filled with zeros) nf.setMinimumFractionDigits (2) / / set mandatory retention of two digits, and use count retention for processing if there are more than two digits (that is, carry conversion for more than two digits) nf.setRoundingMode (RoundingMode.HALF_UP); / / set count retention rules, where rounding nf.setGroupingUsed (false) is used; / / whether to group data with commas (value) / / return value (use the formatting method of the nf object to format the data before returning)} format the data using java.util.Formatter
Data can also be formatted using java.util.Formatter. The specific code is as follows:
Public static String format4 (double value) {return new Formatter () .format ("% .2f", value) .toString () / / using java.util.Formatter 's format method, you can format value values into data in a specified format / /% is formatted characters (similar to C). 2 means to retain two places after the decimal point, f indicates that the value passed in is a floating point / / finally converts the value to a string using the toString method to format the data using String.format (the most common and simple method)
As a text processing tool, String.format provides us with powerful and rich string formatting functions, and it can also format floating-point numbers. The specific code is as follows:
Public static String format5 (double value) {return String.format ("% .2f", value). ToString (); /% is a format character (similar to C). 2 means to retain two decimal places, and f indicates that the value passed in is floating / / and finally converts the value into a string} using the toString method.
The above can be regarded as the easiest way. But it is called common because it can be used directly in print. For example:
Double num = 123.4567899 / System.out.print (String.format ("% .2f", num)); / / used directly in print
These are all the contents of the article "how to keep two decimal places in java". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.