How Java rounds a float to a 2-bit, 4-bit, or other specified number of digits after the decimal point.

Source: Internet
Author: User
Tags mul rounds

How to make float keep two decimal places or decimal placeshttp://meryvn.blog.163.com/blog/static/36962664201173010402629/

Two methods:

Import java.math.*;
......
Method 1:
float F = 34.232323;
BigDecimal B = new BigDecimal (f);
Float F1 = B.setscale (2, bigdecimal.round_half_up). Floatvalue ();
B.setscale (2, bigdecimal.round_half_up) indicates rounding, reserving two decimal places


Method 2:
float scale = 34.236323;
DecimalFormat fnum = new DecimalFormat ("# #0.00");
String Dd=fnum.format (scale);
SYSTEM.OUT.PRINTLN (DD);

How Java rounds a float to a 2-bit, 4-bit, or other specified number of digits after the decimal point.

Previously thought is very easy, has not cared, today suddenly uses, only then discovered, the system does not have such function. Crazy Dizzy, colleagues use the method for, first turn into a String, then take a few of them, and then turn into a float type, (such as: String.valueof (c). substring (0,   String.valueof (c). IndexOf (".") + 3)): I feel so uncomfortable, so I looked for a book to see, the book really did not find, dizzy. To find some methods on the Internet, but also good. The following are (take 2 bits, if you want to take other bits, modify it yourself):

(a): This method is convenient, I use this method

float a = 123.2334f;
Float B = (float) (Math.Round (a*100))/100 (100 here is 2 decimal point, if you want other bits, such as 4 bits, here two 100 is changed to 10000)

(b): This method is also simple, but also to be converted to float type:
Import Java.text.DecimalFormat;
String a = new DecimalFormat ("###,###,###.##"). Format (100.12345);

(c): This can also be used

float ft = 134.3435f;
int scale = 2;//Set Number of bits
int roundingmode = 4;//for rounding, you can choose other methods of rounding, such as tail, etc.
BigDecimal bd = new BigDecimal (double) ft);
BD = Bd.setscale (Scale,roundingmode);
FT = Bd.floatvalue ();

(iv): Personal thoughts, not yet done

is to enlarge 10 of the N-square, into an integer, and then divided by 10 of the N-square to change back to float type, do not know this can not?

The following is not verified, first put up:

There is a problem with the numerical calculation today. The program is as follows:
Double A = (3.3-2.4)/0.1;
System.out.println (a);
You may think the result is simple, not just 9, in fact, the result is: 8.999999998, why? I looked through some information and finally found out why.
Why are floating-point numbers losing precision?
Binary representation of decimal numbers may not be accurate

It is not uncommon for floating-point numbers or double-precision floating-point numbers to be accurately represented. The reason why floating-point numbers are not represented in decimal notation is attributed to the way the CPU represents floating-point numbers. In this case, you may sacrifice some precision, and some floating-point arithmetic will introduce errors. As an example of the above-mentioned case, the binary representation of 2.4 is not exactly 2.4. Instead the nearest binary representation is 2.3999999999999999. The reason is that the floating-point number consists of two parts: exponent and mantissa. The value of a floating-point number is actually calculated from a specific mathematical formula. The loss of precision you encounter will be encountered in any operating system and programming environment.
Note: You can use the binary Coded Decimal (BCD) library to maintain precision. The BCD digital encoding method encodes each decimal digit bit separately.
Type mismatch
You may have mixed floating-point and double-precision floating-point number types. Make sure that all data types are the same when you are doing mathematical operations.
Note: A variable of type float has a precision of only 7 bits, whereas a variable of type double has a precision of 15 bits.
How do I calculate floating-point accuracy?
Simple floating-point type float and double in Java are not capable of operation. It's not just Java, it's also a problem in many other programming languages. In most cases, the result of the calculation is accurate, but try a few more times (you can do a loop) to try out a similar error. Now we finally understand why we have BCD code.
This problem is quite serious, if you have $9.999999999999, your computer is not going to think you can buy 10 dollars of merchandise.
Special currency types are available in some programming languages to handle this situation, but Java does not. Now let's look at how to solve this problem.

Rounded
Our first reaction was to do rounding. The round method in the math class cannot be set to retain several decimals, and we can only do this (keep two bits):
Public double round (double value) {
Return Math.Round (value*100)/100.0;
}
Unfortunately, the above code does not work properly, passing 4.015 to this method it will return 4.01 instead of 4.02, as we saw above
4.015*100=401.49999999999994
So if we're going to do exact rounding, we can't do anything with a simple type.
Java.text.DecimalFormat also does not solve this problem:
System.out.println (New Java.text.DecimalFormat ("0.00"). Format (4.025));
Output is 4.02

BigDecimal
This principle is also mentioned in the book "Effective Java", where float and double can only be used for scientific calculations or engineering calculations, and we use java.math.BigDecimal in business calculations. There are 4 bigdecimal, we do not care about the two that are made with BigInteger, and then there are two of them:
BigDecimal (double val)
Translates a double into a BigDecimal.
BigDecimal (String val)
Translates the String repre sentation of a BigDecimal into a BigDecimal.
The above API brief description is fairly clear, and usually, the one above is easier to use. We may not want to use it, what is the problem? When there is a problem, only to find out which one of the above-mentioned detailed description of the method has such a paragraph:
Note:the results of this constructor can be somewhat unpredictable. One might assume that new BigDecimal (. 1) is exactly equal to. 1, but it's actually equal to. 1000000000000000055511151231 257827021181583404541015625. Because. 1 cannot be represented exactly as a double (or, for that matter, as a bi nary fraction of any finite length). Thus, the Long value that's being passed in to the constructor are not exactly equal to. 1, appearances nonwithstanding.
The (String) constructor, on the other hand, is perfectly predictable:new BigDecimal (". 1") are exactly equal to. 1, as one would expect. Therefore, it is generally recommended and that the (String) constructor being used in preference to this one.
If we need accurate calculation, it is necessary to use string to build BigDecimal! In "effective Java" the example is to use string to build BigDecimal, but the book does not emphasize this point, this may be a small mistake it.

Solution Solutions
Now we can solve this problem, the principle is to use BigDecimal and must use string to build enough.
But imagine, if we're going to do an addition, we need to convert two floating-point numbers to string, then BigDecimal, call the Add method on one of them, pass in another as a parameter, and then transform the result of the operation (BigDecimal) into a floating-point number. Can you endure such a tedious process? Below we provide a tool class arith to simplify the operation. It provides the following static methods, including subtraction and rounding:
public static double Add (Double v1,double v2)
public static double sub (double v1,double v2)
public static double Mul (Double v1,double v2)
public static double div (Double v1,double v2)
public static double div (double v1,double v2,int scale)
public static double round (double v,int scale)
Appendix
Source file Arith.java:
Import Java.math.BigDecimal;
/**
* Because Java's simple type does not accurately operate on floating-point numbers, this tool class provides fine
* Accurate floating-point arithmetic, including subtraction and rounding.
*/
public class arith{
Default division Operation Precision
private static final int def_div_scale = 10;
This class cannot be instantiated
Private Arith () {
}

/**
* provides accurate addition operations.
* @param v1 Summand
* @param v2 Addend
* @return of two parameters and
*/
public static double Add (Double v1,double v2) {
BigDecimal B1 = new BigDecimal (double.tostring (v1));
BigDecimal b2 = new BigDecimal (double.tostring (v2));
Return B1.add (B2). Doublevalue ();
}
/**
* Provides accurate subtraction operations.
* @param v1 minuend
* @param v2 meiosis
* @return The difference of two parameters
*/
public static double sub (double v1,double v2) {
BigDecimal B1 = new BigDecimal (double.tostring (v1));
BigDecimal b2 = new BigDecimal (double.tostring (v2));
Return B1.subtract (B2). Doublevalue ();
}
/**
* Provides accurate multiplication operations.
* @param v1 by multiplier
* @param v2 Multiplier
* @return The product of two parameters
*/
public static double Mul (Double v1,double v2) {
BigDecimal B1 = new BigDecimal (double.tostring (v1));
BigDecimal b2 = new BigDecimal (double.tostring (v2));
Return b1.multiply (B2). Doublevalue ();
}

/**
* Provide (relative) accurate division operation, when the occurrence of an endless situation, accurate to
* After the decimal point 10 digits, the later numbers are rounded.
* @param v1 Dividend
* @param v2 Divisor
* @return two parameters of the quotient
*/
public static double div (Double v1,double v2) {
Return Div (V1,v2,def_div_scale);
}

/**
* Provide (relative) accurate division operations. When an exception occurs, the scale parameter refers to the
* Fixed precision, after which the numbers are rounded.
* @param v1 Dividend
* @param v2 Divisor
* @param scale indicates the need to be accurate to several decimal places.
* @return two parameters of the quotient
*/
public static double div (double v1,double v2,int scale) {
if (scale<0) {
throw New IllegalArgumentException (
"The scale must is a positive integer or zero");
}
BigDecimal B1 = new BigDecimal (double.tostring (v1));
BigDecimal b2 = new BigDecimal (double.tostring (v2));
Return B1.divide (B2,SCALE,BIGDECIMAL.ROUND_HALF_UP). Doublevalue ();
}

/**
* Provides precise rounding of decimal digits.
* @param v need to be rounded to the number
* Retain several @param scale decimal points
* Results after rounding @return
*/
public static double round (double V,int scale) {

if (scale<0) {
throw New IllegalArgumentException (
"The scale must is a positive integer or zero");
}
BigDecimal B = New BigDecimal (double.tostring (v));
BigDecimal one = new BigDecimal ("1");
Return B.divide (ONE,SCALE,BIGDECIMAL.ROUND_HALF_UP). Doublevalue ();
}
};

for Double d = 2.4;
System.out.println (d);//output 2.4, but not 2.3999999999999999?
Read some of the data, when a single output double value, can be correctly displayed in decimal, specifically why, I also indefinitely, but for floating point calculation, floating point calculation refers to the operation of floating point number, which is usually accompanied by the approximation or rounding due to the inability to accurately represent. may be related to Floatingdecimal (d). Tojavaformatstring () of the Double.tostring method
Guess it's about 2.3999999999999999 out of the accuracy of the output, so it's been intercepted.

How Java rounds a float to a 2-bit, 4-bit, or other specified number of digits after the decimal point.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.