In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the methods of keeping decimal places except Double in Java". In daily operation, I believe many people have doubts about the method of Double except retaining decimal places in Java. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "what is the method of keeping decimal places in Double in Java?" Next, please follow the editor to study!
Returns the simple type of
1. Can be rounded
Double d = 114.145 Math.round d = (double) System.out.println (d)
2. BigDecimal.ROUND_HALF_UP means rounding, BigDecimal.ROUND_HALF_DOWN is also rounding, BigDecimal.ROUND_UP means rounding (that is, directly adding 1), and BigDecimal.ROUND_DOWN means removing Mantissa directly.
Double d = 114.145 BigDecimal b = new BigDecimal (d); d = b.setScale (2, BigDecimal.ROUND_HALF_UP). DoubleValue (); System.out.println (d)
Returns the string type of
1.room.00 means that the last two bits are reserved, and it is handled by truncating the unwanted Mantissa directly and not rounding it.
Double d = 114.145 / DecimalFormat df = new DecimalFormat ("# .00"); String str = df.format (d); System.out.println (str)
2.% .2f means that the last two places are reserved and can be rounded.
Double d = 114.145 / String.format ("% .2f", d)
3.RoundingMode.HALF_DOWN means rounding, negative numbers are rounded to absolute values, then negative numbers are rounded, RoundingMode.HALF_UP: they are rounded, negative numbers are rounded to absolute values and then rounded to negative numbers.
Double d = 114.145NumberFormat nf = NumberFormat.getNumberInstance (); / / keep two decimal places nf.setMaximumFractionDigits (2); / / if you don't need to round off, you can use RoundingMode.DOWNnf.setRoundingMode (RoundingMode.UP); System.out.println (nf.format (d))
Add: four ways to keep 2 decimal places in java
Package CodeJava_Leet;import java.math.BigDecimal;import java.text.DecimalFormat;import java.text.NumberFormat;/** * Created by Yechengpeng on 2016-08-14. * / public class Test {public static void main (String [] args) {double d = 756.2345566
/ / method 1: the easiest method, call DecimalFormat class DecimalFormat df = new DecimalFormat (".00"); System.out.println (df.format (d))
/ / method 2: implement System.out.println (String.format ("% .2f", d) directly through the format function of the String class
/ / method 3: implement BigDecimal bg = new BigDecimal (d) through the BigDecimal class; double d3 = bg.setScale (2, BigDecimal.ROUND_HALF_UP). DoubleValue (); System.out.println (d3)
/ / method 4: implement NumberFormat nf = NumberFormat.getNumberInstance (); nf.setMaximumFractionDigits (2); System.out.println (nf.format (d)) through NumberFormat class;}}
At this point, the study of "what are the methods of keeping decimal places in Double in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.