How Java forces the retention of two decimal places
Editor to share with you how Java forcibly retain two decimal places, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
Method 1: String's format method (recommended) double f = 111231.5585 / system. Println (String.format ("% .2f", f)); method 2: DecimalFormat's format method double f = 111231.5585 / DecimalFormat df = new DecimalFormat ("# .00"); System.out.println (df.format (f))
You can understand the following, but you don't have to read it.
Method 3: BigDecimal's setScale method double f = 111231.5585 doubleValue bg = new BigDecimal (f); double F1 = bg.setScale (2, BigDecimal.ROUND_HALF_UP). DoubleValue (); System.out.println (F1); method 4: NumberFormat's setMaximumFractionDigits method double f = 111231.5585 System.out.println nf = NumberFormat.getNumberInstance (); nf.setMaximumFractionDigits (2); System.out.println (nf.format (f)) The above is all the contents of the article "how Java forcibly retains two decimal places". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!