round « double « Java Data Type Q&A

1. How to resolve a Java Rounding Double issue

Seems like the subtraction is triggering some kind of issue and the resulting value is wrong.

double tempCommission = targetPremium.doubleValue()*rate.doubleValue()/100d;
78.75 = 787.5 * 10.0/100d
double netToCompany = targetPremium.doubleValue() - tempCommission;
708.75 = 787.5 - ...

2. Why does Math.round return a long but Math.floor return a double?

Why the inconsistency?

3. What's a java equivalent for std::round(double)?

In cpp file I have std::round(double) Please can I know the equivalent code in Java Edit: I am already using java.lang.Math.round(double) and getting match in 99% cases. But in some places iam getting ...

4. how to round off a double value to the nearest 0.05 in java

i want to round off a double value to its nearest 0.05 in java. eg: 54.625 to 54.65 or 32.1885 to 32.19 etc.

5. Rounding a double

Hey guys, I am trying to round to 3 decimal places. I used the following code.

this.hours = Round(hours + (mins / 60), 3);
But it's not working. Where have I gone wrong? Thanks

6. Double value to round up in Java

I have a double value = 1.068879335 i want to round it up with only two decimal values like 1.07. I tried like this

DecimalFormat df=new DecimalFormat("0.00");
String formate = df.format(value);
double finalValue = Double.parseDouble(formate) ...

7. Using double in loops. Possible problem with rounding

I've got a little question concerning a FOR cycle I've seen today. It looks like this :

for (double i = 0.0; i < 1.0; i += 0.1) {  

8. Rounding the Double Values

Rounding Issue

For Example
  1. 598.59 ya 591.45 to be convert 600.
  2. 541.0 to be convert 550.
  3. 541.59 to be convert 550. Just check the last 2 digit ignore the decimal value. if last 2 digit in between ...

9. How to avoid rounding errors of double values in conditionals?

At the moment this is what my code looks like:

if (!flipped){
    scaleX -= 0.1;
    if (scaleX < 0.0001){

10. Round a double to 3 significant figures

Does anybody know how I can round a double value to 3 significant figures like the examples on this website

11. Rounding a number up in Java

I dont get how rounding numbers up to certain decimal places I looked everywhere tried every thing currently I have my program to round up to a whole number with double rACT = ...

12. double "rounding"

I have two questins related: * First of all, I wanted to know if there is any function which rounds a double number to a double with a concrete number of decimals. For example, if I have the number 82.0716987 and I want to round it to two decimals I should get 8.,07. does it exist that function?? *As, by now, ...

13. Rounding a double

Hi, What is the standard way to round a double at a precision of x? I know there is a way when you multiply your double with a power of 10, then do a cast to int and then divide with the same power of 10. Con of this method: when your double is bigger than the maximum value for integer, ...

14. Rounding up double value

Hie, I need to to round double value to a 2 decimal point value.However, the value is to be round up instead round down as per default. E.g : 2.142 -> 2.15 (default is 2.14) or 2.1425 -> 2.15 (default is 2.14) Code shown below return a round down value. *********************************************** try { DecimalFormat df = new DecimalFormat("RM#,###,###,###.00"); if (!val.equals(null) || ...

15. Rounding of doubles for infinitely long numbers

Hi, I am trying to uderstand the maths behind doubles (and floats). It seems that if you divide 1.0 by 3, assign a double reference to this number and then multiply the result by 3, then you get 1.0 again. Obviously this is what you want and the answer is correct. However, I would be grateful if someonoe could explain how ...

16. Round double up with 2 digits....

double d = 4.85999999999; long l = (int)Math.round(d * 100); // truncates d = l / 100.0; Keep in mind though that the double will become incorrect again if you do a lot of math with it. For example: double d = 4.85999999999; System.out.println(d); long l = (int)Math.round(d * 100); // truncates System.out.println(l); d = l / 100.0; System.out.println(d); for (int ...

17. rounding a double

Aha! You have learned how floats and doubles are not very accurate! Here, try this: Math.floor( number * 100 + .5 ) / 100 What this code does, of course, is multiply the number by 100, then add .5, then divide by 100 to get back to the hundredths place. Try to work this code out by hand with a calculator ...

18. rounding a double value

19. Double rounding

Originally posted by Edward Chen: we found the rounding is related to the integer, why ? what is the maximum of the decimal digits, 16 or 15 ? what is the rule here? Primitive numbers (double, int, float etc.) are not stored in decimal. They are stored in binary. Therefore, there is not an exact number of decimal digits available. It ...

20. How to round last 2 digits of a double

21. Double Value Round Problem

22. double value round off

23. problem in rounding the double value

24. doubles & rounding

I'm working on an ASCII version of battleship, and as part of the project, the hit/miss stats have to be printed out at the end. Because this is for a computer science course, my output has to be identical to the required output. My program prints this: Hit ratio: 66.66666666666667% When it should be printing: Hit ratio: 66.66666666666666% and.. My Program: ...

25. rounding a double

Hi all, as you can tell from my question I am new to JAVA. So far I love it. I need to round a double to 2 places but the Math.round method rounds to an integer. Does someone have a good way to round to 2 places? Thanks Message was edited by: LTLhawk

26. Rounding Doubles

I am working on a simple code and need help rounding a double to two decimal places. I have looked around and tried the BigDecimal class, but I'm not sure how to exactly implement it. Any help would be great. I need to round a double for a method that calculates the MPG of a car (simple assignment). Thanks.

27. Rounding Doubles

28. Rounding double for string purposes

Hello Forum, I have small problem: Let say I need to convert meters to feet. I have jTextFieldMeter for inputing the meters and jButtonCalculateFeet to calculate to feet. I also have jTextArea where resultS ARE displayed. Now, calculation is perfect (button action): double feet = Double.parseDouble(jTextFieldMeter) * 3.28083; Here is the problem: How do I display each result in jTextArea in ...

30. Need some help in Rounding a double value to a whole number

double d = 62222.22222222;long l = (int)Math.round(d * 100); // truncatesd = l / 100.0; double d = 62222.22222222; System.out.println(d); long l = (int)Math.round(d * 100); // truncatesSystem.out.println(l); d = l / 100.0;System.out.println(d); for (int i = 0; i < 1000; i++) { d -= 0.1;} for (int i = 0; i < 1000; i++) { d += 0.1;}System.out.println(d); regards- Julie ...

31. Changing rounding precision of a double

32. Rounding a double number issue

33. Unexpected rounding of double

Well, let's see: I can not replace a+b with BigDecimal.add all around a ~4000 classes program. I understand java does not treat 2500000.7549999998882 as I would have expected from a double, since it just store 2500000.755 But, what I need here, and this question is about, is to know the details of this limitation in order to take the corresponding actions ...

34. How to round a double

35. doubles & rounding

I'm working on an ASCII version of battleship, and as part of the project, the hit/miss stats have to be printed out at the end. Because this is for a computer science course, my output has to be identical to the required output. My program prints this: Hit ratio: 66.66666666666667% When it should be printing: Hit ratio: 66.66666666666666% and.. My Program: ...

36. Is there a problem with this method to round a double?

WTJ, please enlighten us why the method should not be static. The method doesn't use, retain, or modify any state. And would you then please enlighten us as to which buffer will overflow. When FP overflows you get a NaN or a POSITIVE_INFINITY or NEGATIVE_INFINITY. @OP: contrary to the nonsense above, this will operate OK as is: it might throw an ...

37. Rounding a double to a format