round « bigdecimal « Java Data Type Q&A





1. Rounding a Java BigDecimal to the nearest interval    stackoverflow.com

I have a BigDecimal calculation result which I need to round to the nearest specified interval (in this case it's the financial market tick size). e.g. Price [Tick Size] -> Rounded Price

100.1 ...

2. round BigDecimal to nearest 5 cents    stackoverflow.com

I'm trying to figure out how to round a monetary amount upwards to the nearest 5 cents. The following shows my expected results

1.03     => 1.05
1.051   ...

3. BigDecimal rounding question    stackoverflow.com

I would like to be able to round up any number to quater of the whole number. For example:

100.33 -> 100.50
100.12 -> 100.25
100.66 -> 100.75
100.99 -> 101.00
100.70 -> 100.75
100.00 -> 100.00
100.25 -> ...

4. BigDecimal setScale and round    stackoverflow.com

What is the difference between this two call? (Is there any?)

// 1.
new BigDecimal("3.53456").round(new MathContext(4, RoundingMode.HALF_UP));
// 2.
new BigDecimal("3.53456").setScale(4, RoundingMode.HALF_UP);

5. Java BigDecimal: Round to the nearest whole value    stackoverflow.com

I've been trying to figure this out, and the previously related questions on SO aren't helping me out either.. I need the following results

100.12 -> 100.00
100.44 -> 100.00
100.50 -> 101.00
100.75 -> ...

6. Java - How to round up float (or BigDecimal ) value by 0.5?    stackoverflow.com

Seems simple question but I really suck at math and few examples online I've searched seems not working for me. (the result just return the same value as input etc) For instance.. ...

7. Efficient BigDecimal round Up and down to two decimals    stackoverflow.com

In java am trying to find an efficient way to round a BigDecimal to two decimals, Up or Down based on a condition.

 IF condition true then:
    12.390 ...

8. Different results are obtained while rounding decimals    stackoverflow.com

double radius = 5;
double area = Math.PI * Math.pow(radius, 2);            
// System.out.println(area);

BigDecimal bd = new BigDecimal(area).setScale(2, HALF_UP);
// BigDecimal bd = ...

9. Advanced rounding options with BigDecimal    stackoverflow.com

I am writing a program that has to round very large and precise numbers (possibly many more significant digits than even double could handle), and am using BigDecimals. Is there a way ...





10. Strange rounding behaviour with BigDecimal?    stackoverflow.com

What would be printed to console and why? 1.

BigDecimal BigDecimalNum = new BigDecimal("0.0774");
System.out.println(BigDecimalNum.doubleValue() * 100.00);
2.
BigDecimal BigDecimalNum2 = new BigDecimal("0.0774");   
System.out.println(BigDecimalNum2.multiply(new BigDecimal("100.00")));

11. Which rounding mode to be used for currency manipulation in java?    stackoverflow.com

I have read on java site to use BigDecimal for currencies. http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html But what rounding mode we should use with? which is the most appropriate one and most widely us

12. BigDecimal not rounding properly in some cases?    coderanch.com

I have the following code to round a decimal to 2 decimal places and round half up. BigDecimal bd = new BigDecimal(d); return bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); During junit testing I was kind of amzed to find this failed for some numbers. For example, If I have 0.235 I'd expect the value to be 0.24 when it comes back however it doesn't "round ...

13. Rounding BigDecimal Calculation Yielded Wrong Result    coderanch.com

Hi James, I think it's still possible to use BigDecimal for the calculation if the scale value is set higher. Then round off the final value to 2 or 3 decimal places. BigDecimal total = new BigDecimal("415"); BigDecimal rate = new BigDecimal("1.2"); BigDecimal value = rate.divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP); System.out.println(value); // output is 0.01 when scale set to 2 System.out.println(total.multiply(value)); // ...

14. BigDecimal rounding problem    coderanch.com

referrinf to following code, import java.math.BigDecimal; public class RoundTest { public static void main(String args[]) { System.out.println(round(4.4,0)); System.out.println(round(4.5,0)); System.out.println(round(4.6,0)); /* output 4.22 4.22 <-- 4.23 expected output by me 4.22 4.23 <-- 4.23 */ } private static double round(double value, int noOfDecimalDigits) { BigDecimal bd = new BigDecimal(value); bd = bd.setScale(noOfDecimalDigits, BigDecimal.ROUND_HALF_EVEN); return bd.doubleValue() ; } } but my belief is ...

15. BigDecimal and the lack of information about rounding    coderanch.com

We ran up against a rounding "problem" in Java which we were unaware of. We have since discovered that if we want exact results we need to use the BigDecimal API or we could use int or long if we want to keep track of the decimal (look at Joshua Bloch's item #31 in "Effective Java"). However, my post isn't technical, ...

16. BigDecimal Rounding    coderanch.com

Hi, I'm getting a different value than expected using BigDecimal.setScale() method and I wanted to see if someone can tell me why. Basically, (and this is not currency) a number rounded to 2 digits right of the decimal point. I also choose ROUND_HALF_DOWN so that <=5 is rounded down and >=5 is rounded up. While JUniting, I set a test case ...





17. BigDecimal: divide round help    coderanch.com

19. BigDecimal round value ?    coderanch.com

20. Rounding in BigDecimal    coderanch.com

I used the class BigDecimal and used the field ROUNDHALFUP in order to round up a number, but it didn't round up. More specifically, the number was the double obtained by dividing 1942 by 40. If you divide 1942 by 40 on paper, you get 48.55 exactly, so I figured that using ROUNDHALFUP would give me 48.6 but instead I got ...

22. How to avoid rounding while BigDecimal division    coderanch.com

Hi, I am trying to divide two BigDecimal numbers like this BigDecimal startDateIndexPointValue = new BigDecimal(startDateIndexedValue.toString()); BigDecimal endDateIndexPointValue = new BigDecimal(endDateIndexedValue.toString()); // where startDateIndexPointValue = 2028, endDateIndexPointValue = 2047 BigDecimal intermediateValue; intermediateValue = endDateIndexPointValue.divide(startDateIndexPointValue, 5, RoundingMode.HALF_UP); The problem is that this using this precision and using the rounding mode, I am not getting the exact intermediate value. I need the intermediate ...

23. Rounding BigDecimal Number    coderanch.com

I have a requirement where the input number is 30.5 and break up scale is 10. so the out put should be as 10+10+10+0.5 Here, a) In line 28, I am trying to add the values in variable totalIncome. But it is not getting populated. b) Each time while rounding the number, I am dividing it with 1 and setting the ...

24. BigDecimal Rounding    forums.oracle.com

im using BigDecimal to round a value say 2.345486768 setScale(3,RoundingMode.HALF_UP) will give me 2.345 what java does is it multiplies 2.345486768 with 10^3 and adds 0.5 to it ie; 2.345486768*1000=2345.496768+0.5= 2345.86768 so it returns 2.345 what i wanted is round from the last decimal 2.345486768 least decimal value is 8 so Rounding it to 1 decimal place less it should be ...

25. Rounding issue for BigDecimal value    forums.oracle.com

26. BigDecimal.divide rounding rounds down instead of up    forums.oracle.com

Sticking with update 13, the last working release, is the only work around that we can live with. However I want to make sure that this bug will not be forgotten. The last I heard from Sun was an automated response saying that I would hear from them if the bug was accepted or marked duplicate and that they cannot respond ...

27. BigDecimal, division method and Rounding    forums.oracle.com

Yes, I did. The concept of RoundingMode is not very easy to catch at first. I have been using the BigDecimal for a long time now but trying much to avoid the division complexities that could crop up. Also to note, the MathContext looks even harder to grasp so if I may say, many developer seem more comfortablet to get the ...

28. BigDecimal: how to keep 2 fraction digits, round up.    forums.oracle.com

Hi, To calculate money amount, we need to have 2 fraction digits. For example, 8888.567 --> 8888.57 99998888.865 ---> 99998888.87 0.982 --> 0.98 for method round(MathContext), the mathContext specifies total number of digits, not fraction digits. The workaround we have to use: amount.divide(new BigDecimal("1"), 2, RoundingMode.HALF_UP); what is the right way to achieve 2 fraction digits/half-UP? Thanks for help. Dave

30. Issue with BigDecimal.ROUND_HALF_UP    forums.oracle.com

31. sample java program to round bigdecimal number to its nearest number    forums.oracle.com

hi, i want to round a BigDecimal number to its nearest number eg. if my calculation result is 75.3 then it has to be rounded to 75 if my calculation result is 75.6 then it has to be rounded to 76 if my calculation result is 75.5 then it has to be rounded to 76 can anyone help me out resolving ...

32. BigDecimal scale, rounding and ArithmeticException    forums.oracle.com

Can anyone refer me to a good article or site where the appropriate use of the BigDecimal Class is shown. I keep having a lot of ArithmeticException especially when doing division or setting scale. It is becoming frustrating that I refer to doing calculation with double and changing them back to BigDecimals. Thanks in advance Michael

33. BigDecimal rounding    forums.oracle.com

34. BigDecimal rounding problem - Interesting    forums.oracle.com

35. BigDecimal rounding    forums.oracle.com

Hi all i know that this problem was discussed but i'm not able to resolve my problem. i know that if i have to divide a bigdecimal the best is to divide it by a new BigDecimal("100"). My problem is that i have to multiply 2 BigDecimal and scale the result to 2 decimal precision. My result is -37.34499999999999975131004248396493494510650634765625 i apply ...

36. double, BigDecimal and rounding    forums.oracle.com

Hello, I am making a program that contain a part of accounting. I have real problems of rounding. For example : BigDecimal nbt = new BigDecimal(1.2); System.out.println(nbt); display: 1.1999999999999999555910790149937383830547332763671875 instead of 1.20 and double number= 1.2; int firsPart= (int)number; int secondPart= (int)((number- (double)firsPart) * 100); System.out.println(firsPart+"/"+secondPart); display : 1/19 instead of 1/20 So what is the solution ?