Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the accuracy of preserving decimal places in Java Float

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to realize the precision of keeping decimal places in Java Float? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Float retains decimal precision DecimalFormat decimalFormat=new DecimalFormat (".00"); return Float.valueOf (super.getDecimalFormat () .format (new BigDecimal (handleTime); Float floating-point data retains two decimal places

Two methods have been used: DecimalFormat and Math.round ().

Usage:

1 、 DecimalFormat

String java.text.NumberFormat.format (double number) method

Float f = 0.5555fumbDecimalFormat df1 = new DecimalFormat ("# .00"); / / keep two decimal places. If it is a few decimal places, the zero before the decimal point will not be displayed, and the zeros after the decimal point will retain a few digits df1.format (f). # indicates that the place does not need to be displayed if it is 0, and 0 means that the place is still displayed if it is 0.

The definition of a function is:

So, the parameter is double, or you can pass float (implicit conversion), and the end result is the String type.

2. Math.round ():

Int java.lang.Math.round (float a) method

Float f = 1.222f / Math.round (f * 100) / 100f / Math.round / multiply by 100and then divide by 100 to convert to floating-point type / * * keep as many decimal places as you want * * Note that the Math.round () method passes the float type! , /

Both methods are rounded.

If it is a floating-point type, it is recommended to use the Math.round method, or you can diy the code according to your needs.

For more information, please see the code comments and console output: (including the format of decimalFloat and the implementation logic of Math.round function)

Package testMap; import java.text.DecimalFormat; public class TestFloat {public static void main (String [] args) {/ / TODO Auto-generated method stub / / rounding retains two digits float f = 0.5555f / / decimalFormat converts double type data to a string and formats / / # to indicate that this bit does not need to be displayed if it is 0, and 0 means that this bit is / / format function: String java.text.NumberFormat.format (double number) DecimalFormat df1 = new DecimalFormat ("# .00") / / the first zero is not displayed, that is, 0.1 DecimalFormat df2 = new DecimalFormat ("0.00"); / / the first 0 is displayed, that is, 0.1 is displayed as 0.1 System.out.println ("- DecimalFormat-"); System.out.println ("df1==" + df1.format (f)) System.out.println ("df2==" + df2.format (f)); System.out.println (df1.format (f). GetClass ()); / / String type System.out.println (Float.parseFloat (df1.format (f); / / converted to float type System.out.println (String.valueOf (df1.format (f) / / System.out.println (Float.toString (df1.format (f); / / convert to String type f = 0.595f / / the Math.round () method multiplies the floating-point type data by how many powers of 10, then divides + 0.5 by how many powers of 10, and takes the number of places after the decimal point / / if multiplied by 1000, take the System.out.println ("- Math.round () -") after the decimal point. System.out.println (Math.round (f * 100f) / 100f); / / if the end is 0, it is automatically omitted and / / System.out.println (df1.format ("1.2")) is not displayed; / / the parameter must be numeric String java.text.NumberFormat.format (double number) System.out.println (Float.toString (f)) / / convert to String output effect System.out.println (Float.toString (f)); / / convert to String output effect System.out.println ("- Math.round () positive non-special value implementation logic -"); f = 11.115111f Int b = (int) (f * 1000.5); float a = b / 100f; System.out.println ("averse =" + a); System.out.println ((int) (f * 1000.5) / 100f); f =-12.115f System.out.println ("negative" + Math.round (f * 100f) / 100f); f =-12.116f; System.out.println ("negative" + Math.round (f * 100f) / 100f) System.out.println ("- Math.round () negative non-special value implementation logic -"); int c = (int) (f * 1000.5); float d = c / 100f; System.out.println ("d =" + d) System.out.println ((int) (d * 1000.5) / 100f);}}

Console output:

The screenshot is as follows:

-the following is the console output-(the same as above, in case the picture is lost)

-DecimalFormat-

Df1==.56

Df2==0.56

Class java.lang.String

0.56

. 56

-Math.round ()-

0.6

0.595

0.595

The positive non-special value of Math.round () implements the logic-

A total of 11.12

11.12

Negative number-12.11

Negative number-12.12

The negative non-special value of-Math.round () implements the logic-

Dazzling music 12.12

-12.12

By the way, paste the code of NumberFormat.formart ():

/ * Specialization of format. * * @ param number the double number to format * @ return the formatted String * @ exception ArithmeticException if rounding is needed with rounding * mode being set to RoundingMode.UNNECESSARY * @ see java.text.Format#format * / public final String format (double number) {/ / Use fast-path for double result if that works String result = fastFormat (number) If (result! = null) return result; return format (number, new StringBuffer (), DontCareFieldPosition.INSTANCE) .toString () } this is the answer to the question about how to realize the decimal precision of Java Float. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report