About 34,800 results
Open links in new tab
  1. math - Getting wrong answer in double arithmetic in Java - Stack …

    Jan 7, 2013 · I'm writing a program in Java that deals with a lot of double arithmetic. I eventually get to the point where I need to add 0.6666666666666666 and -0.666666666666667. However, the answer that I get is -3.3306690738754696E-16. In other words, double d1 = 0.6666666666666666; double d2 = -0.666666666666667; System.out.println(d1 + d2);

  2. math - How to resolve a Java Rounding Double issue - Stack …

    Jun 13, 2016 · As a previous poster suggested, When you are doing numeric calculations, use java.math.BigDecimal. However, there is a gotcha to using BigDecimal. When you are converting from the double value to a BigDecimal, you have a choice of using a new BigDecimal(double) constructor or the BigDecimal.valueOf(double) static factory method. Use the static ...

  3. java - Are arithmetic operations on double variables holding …

    Jun 8, 2015 · double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in section 4.2.3 of the Java Language Specification. For decimal values, this data type is generally the default choice.

  4. math - Java Double/Integer - Stack Overflow

    Aug 25, 2012 · Math.round() will help you here. Returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long.

  5. java - Adding and subtracting doubles are giving strange results ...

    In Java, double values are IEEE floating point numbers. Unless they are a power of 2 (or sums of powers of 2, e.g. 1/8 + 1/4 = 3/8), they cannot be represented exactly, even if they have high precision.

  6. java - Using Math.round to round to one decimal place ... - Stack …

    Mar 5, 2014 · The Math.round method returns a long (or an int if you pass in a float), and Java's integer division is the culprit. Cast it back to a double, or use a double literal when dividing by 10. Either: double total = (double) Math.round((num / sum * 100) * 10) / 10; or. double total = Math.round((num / sum * 100) * 10) / 10.0; Then you should get. 27.3

  7. Rounding a double to turn it into an int (java) - Stack Overflow

    The Math.round function is overloaded When it receives a float value, it will give you an int. For example this would work. int a=Math.round(1.7f); When it receives a double value, it will give you a long, therefore you have to typecast it to int. int a=(int)Math.round(1.7); This is done to prevent loss of precision.

  8. java - Round a double to 2 decimal places - Stack Overflow

    May 11, 2010 · For example, double value is 3.72 and if I use format() function, new double value changes 3,72 and If I wanna set this new value to double property, it will be throwed exception of NumberFormatException: For input string: "3,72". But you got …

  9. java - Integer division: How do you produce a double? - Stack …

    Cast one of the integers/both of the integer to float to force the operation to be done with floating point Math. Otherwise integer Math is always preferred. So: 1. double d = (double)5 / 20; 2. double v = (double)5 / (double) 20; 3. double v = 5 / (double) 20; Note that casting the result won't do it.

  10. java - Double vs. BigDecimal? - Stack Overflow

    Aug 5, 2010 · Saying that "BigDecimal is an exact way of representing numbers" is misleading. 1/3 and 1/7 can't be expressed exactly in a base 10 number system (BigDecimal) or in base 2 number system (float or double). 1/3 could be exactly expressed in base 3, base 6, base 9, base 12, etc. and 1/7 could be expressed exactly in base 7, base 14, base 21, etc. BigDecimal advantages are that it is arbitrary ...

Refresh