In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Java how to retain two decimal places, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
First, use BigDecimal, keep the two places after the decimal point public static String format1 (double value) {BigDecimal bd = new BigDecimal (value); bd = bd.setScale (2, RoundingMode.HALF_UP); return bd.toString ();} second, use DecimalFormat, keep the two places after the decimal point public static String format2 (double value) {DecimalFormat df = new DecimalFormat ("0.00"); df.setRoundingMode (RoundingMode.HALF_UP); return df.format (value) Using NumberFormat, leave public static String format3 (double value) {NumberFormat nf = NumberFormat.getNumberInstance (); nf.setMaximumFractionDigits (2); / * setMinimumFractionDigits set to 2 * * if not, return 100 * * instead of 100.00 * / nf.setMinimumFractionDigits (2) when the value of value is 100.00; nf.setRoundingMode (RoundingMode.HALF_UP) / * * if the format you want to output is separated by commas, you can set it to true * / nf.setGroupingUsed (false); return nf.format (value) } fourth, use java.util.Formatter, retain two places after the decimal point public static String format4 (double value) {/ * *% .2f% represents any number of decimal places before the decimal point 2 represents two decimal places after the result is f represents floating point * / return new Formatter (). Format ("% .2f", value). ToString (); fifth, use String.format to achieve. Public static String format5 (double value) {return String.format ("% .2f", value). ToString ();} VI. Expand knowledge
As a text processing tool, String.format provides us with powerful and rich string formatting functions.
Format floating-point numbers
The placeholder format is:% [index$] [identity] * [minimum width] [. Precision] conversion symbol
Double num = 123.4567899 System.out.print (String.format ("% f% n", num)); / / 123.456790 System.out.print (String.format ("a% n", num)); / / 0x1.edd3c0bb46929p6 System.out.print (String.format ("% g% n", num)); / / 123.457
Available identifiers:
Left-aligned within the minimum width and cannot be used with the 0 flag.
0, if the content length is less than the minimum width, fill it with 0 on the left.
# for octal and hexadecimal, add 0x before octal and before hexadecimal.
+, the result always contains a + or-sign.
Spaces, positive numbers preceded by spaces, negative numbers preceded by-signs.
Use only with decimal, separated by every 3 digits.
If the result is negative, it is enclosed in parentheses and no symbol is displayed.
Available conversion characters:
B, Boolean type, as long as the argument is a non-false Boolean type, it is formatted as the string true, otherwise it is the string false.
N, a platform-independent newline character, can also be obtained through System.getProperty ("line.separator").
F, floating point (decimal). Nine significant digits are displayed and are rounded. Such as 99.99.
A, floating point number (hexadecimal).
E, exponential type. Such as 9.38e+5.
G, floating-point number (shorter than% fcent% a, displays 6 significant digits and is rounded)
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.