In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 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 problems caused by the addition of Java Double". In the daily operation, I believe many people have doubts about the problems caused by the addition of Java Double. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "what are the problems caused by the addition of Java Double?" Next, please follow the editor to study!
The raising of the problem of strange things arising from the addition of Java Double
What do you see when you compile and run the following program
Public class test {public static void main (String args []) {System.out.println; System.out.println (1.0-0.42); System.out.println (4.015 * 100); System.out.println (123.3 / 100);}}
You read it right! It did turn out to be
0.060000000000000005
0.5800000000000001
401.49999999999994
1.2329999999999999
The simple floating point types float and double in Java cannot operate. This problem exists not only in Java, but also in many other programming languages. In most cases, the results of the calculation are accurate, but a few more attempts (you can do a loop) can make an error similar to the one above. Now I finally understand why there is a BCD code.
This problem is quite serious. If you have 9.999999999999 yuan, your computer will not think that you can buy goods worth 10 yuan.
Special currency types are provided in some programming languages to handle this situation, but Java does not. Now let's see how to solve this problem.
Solution
Now we can solve this problem, the principle is to use BigDecimal and be sure to use String to build it.
But imagine that if we want to do an addition operation, we need to first convert two floating-point numbers to String, and then generate BigDecimal, call the add method in one, pass in the other as an argument, and then convert the result of the operation (BigDecimal) to a floating-point number. Can you stand such a tedious process? Next we provide a utility class Arith to simplify the operation. It provides the following static methods, including addition, subtraction, multiplication and division, 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); package org.nutz.mvc.core; import java.math.BigDecimal Public class Arith {/ / source file Arith.java: / * because the simple type of Java cannot accurately operate on floating-point numbers, this utility class provides accurate floating-point operations, including addition, subtraction, multiplication and division, and rounding. * / / default division precision private static final int DEF_DIV_SCALE = 10; / / this class cannot instantiate private Arith () {} / * * to provide precise addition operations. * * @ param v1 * is added * @ param v2 * plus * @ return the sum of the two parameters * / 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 precise subtraction. * * @ param v1 * subtracted * @ param v2 * minus * @ return the difference between the 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. * * @ param v1 * multiplier * @ param v2 * multiplier * @ 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 () } / * provides (relatively) precise division operation, which is accurate to 10 digits after the decimal point when inexhaustible division occurs, and subsequent numbers are rounded. * * @ param v1 * Divisor * @ param v2 * Divisor * @ return the quotient of the two parameters * / public static double div (double v1, double v2) {return div (v1, v2, DEF_DIV_SCALE);} / * * provides (relative) precise division. When an inexhaustible situation occurs, the precision is specified by the scale parameter, and the subsequent numbers are rounded. The * * @ param v1 * divisor * @ param v2 * divisor * @ param scale * indicates that it needs to be accurate to a few places after the decimal point. * @ return quotient * / public static double div (double v1, double v2, int scale) {if (scale < 0) {throw new IllegalArgumentException ("The scale must be 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 places. * * @ param v * numbers that need to be rounded * @ param scale * the result of rounding several digits after * @ return rounding * / public static double round (double v, int scale) {if (scale < 0) {throw new IllegalArgumentException ("The scale must be 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 ();}}; String test = "40.61, 18588.73, 29925.07, 7986.06, 18639.19, 25914.32, 32907.74, 34165.89,9724.7,52777.92"; String [] arr = test.split (","); double sum = 0 For (int iTuno Bandi)
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.