preface
Recently, in an interview, I came across such a question: When I was asked about the project part, the interviewer asked me: What data types do you use for scores, amounts and other numbers used in your project? I didn’t think too much and blurted out: Double! Interviewer: Why not use float?
When I heard this question, MY mind suddenly felt confused and I answered: It was easy to use double, so I used it. After the interview, I couldn’t help smiling when I heard my answer when listening to the tape replay (remote online interview), and then I reviewed and deepened the content of this piece.
The difference between double and float
The main differences between float and double are as follows:
1) Different significant digits
The single – precision floating – point number has 8 significant digits
The valid double – precision floating – point number is 16 bits
This means that a double is more accurate than a float because the significant number of digits is different.
2) The value ranges are different
The range of single-precision floating point numbers is -3.40e +38 to 3.40E+38
The range of double – precision floating – point numbers is -1.79e +308 to 1.79E+308
Whereas $3.40 +38 means 3.4*10 ^ 38, + 1.79E+308 means 1.79*10 ^ 308, so double is much larger than float
3) Different number of bytes are occupied in memory
A single-precision floating-point number takes up 4 bytes in memory
A double – precision floating-point number takes up 8 bytes in memory
That is, a double takes up more memory than a float
4) Different processing speeds in the program
In general, the CPU can process single-precision floating-point numbers faster than double-precision floating-point numbers
The default decimal in the program is double, so if you want to use float, you have to force it
public static void main(String[] args){
float a = 1.1;
}
Copy the code
For example, if I write the above code, the program will compile an error, the correct way to write should be the following code:
public static void main(String[] args){
float a = (float)1.1;
float b = 1.1 f;
}
Copy the code
Manually strong or add f after decimal to indicate float (f is case insensitive)
One thing to note when using float: float is an 8-bit significant number, as in the following code:
public static void main(String[] args){
float a = 1.11111111111 f;
System.out.println(a);
}
Copy the code
The final output is 1.1111112
So there’s a question, whether the ninth digit is greater than 5 or not, it’s going to be evaluated by 1 toward the eighth digit.
So that’s the difference between a double and a float
Which data type should be used for the amount?
In summarizing the difference between double and float, I realized that in real development, the amount of money is not stored as DOUBLE or float as I thought before. Why? Let’s look at the following code:
public static void main(String[] args) {
double a=0.03;
double b=0.02;
double c=a-b;
System.out.println(c);
}
Copy the code
The result of this code, most people would think, must be 0.01! Since float and double are floating-point numbers, floating-point operations are often accompanied by approximations and rounding that can’t be accurately represented, so the result is slightly off. There is absolutely no deviation in the calculation of the amount involved.
So how do you represent the amount?
There are two solutions: the first is to store the amount as an integer in minutes or centimetres, and the second is to use a data type like BigDecimal to represent the amount.
As for the first one, I am currently using it when writing projects, while the second one has not been tried yet.
conclusion
The interviewer asked a small question should have so much mystery and knowledge, can not help but let me blush, in the final analysis or their knowledge is not wide enough. However, it is also a small harvest in the interview, can find their own shortcomings and timely complement.
The interviewer asked me which data type I should use to store the amount of money