i have a function that calculates data that creates a BigDecimal format variable and im trying to round up or down accordingly with the results. For instance:
16.543242123 should be 16.5
3.35 should be 3.4
6.3456 should be 6.3
and so on, if the 3rd digit is equal to 5 or above to round up the 2nd digit, if its equal to 4 and below, to remove the 3rd digit. This does not absolutely need to be BigDecimal, i can figure out converting the numbers after i manage to do the rounds up.
Thanks in advance.
This should do it:
BigDecimal bg1 = new BigDecimal(16.543242123);
bg1 = bg1.setScale(1, BigDecimal.ROUND_HALF_EVEN);
System.out.println(bg1.toString()); //16.5
BigDecimal bg2 = new BigDecimal(3.35);
bg2 = bg2.setScale(1, BigDecimal.ROUND_HALF_EVEN);
System.out.println(bg2.toString()); //3.4
BigDecimal bg3 = new BigDecimal(6.3456);
bg3 = bg3.setScale(1, BigDecimal.ROUND_HALF_EVEN);
System.out.println(bg3.toString()); //6.3
BigDecimal bd = new BigDecimal("16.553242123");
BigDecimal format = bd.setScale(1, RoundingMode.HALF_UP);
Related
I know this question has been asked many times but I'm stuck and I don't know what the correct solution should be. I'm writing a program and there's a lot of numbers multiplication. The result must be rounded to 2 decimal places but sometimes the result is not correct.
For example:
I have two doubles v1=99.338 and v2=732.5.
I want to multiply them and have the result rounded to 2 decimal places.
The correct result is 72765.085 so after rounding it should be 72765.09 however in computer the result is 72765.08499999999 and all the rounding methods give 72765.08 which is obviously wrong.
For example
double x= v1 * v2; //x=72765.08499999999
DecimalFormat dec= new DecimalFormat("0.00");
decsetRoundingMode(RoundingMode.HALF_UP);
String v = dec.format(x);
gives 72765.08. The RoundingMode.CEILING in this example works OK but with other values for v and v2 is wrong.
NumberFormat nf= NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
nf.format(x);
gives 72765.08.
BigDecimal big = new BigDecimal(x, setScale(2, RoundingMode.HALF_UP);
double v = decWartosc.format(x);
gives v=72765.08.
Any other ideas?
I've tried this in C# and the result is correct.
You must use BigDecimal for the calculation itself.
BigDecimal exact = BigDecimal.valueOf(v1).multiply(BigDecimal.valueOf(v2));
BigDecimal big = exact.setScale(2, RoundingMode.HALF_UP);
System.out.println(big.toString());
First of all 99.337 * 732.5 = 72764.3525, and NOT 72765.085.
Now, this code:
BigDecimal b1 = new BigDecimal("99.337");
BigDecimal b2 = new BigDecimal("732.5");
BigDecimal result = b1.multiply(b2).setScale(2, RoundingMode.HALF_UP);
System.out.println(result);
Outputs:
72764.35
I have a requirement where I need to divide one BigDecimal number by 100 and show the exact amount that comes up without removing trailing zeros. But zeros are getting trimmed by default. How do I prevent that?
BigDecimal endDte = new BigDecimal("2609.8200");
BigDecimal startDte = new BigDecimal("100");
BigDecimal finalPerformance = endDte.divide(startDte);
System.out.println(finalPerformance.toString());
Output: 26.0982
Expected: 26.098200
What you want is formatting as those 0 does not add to value. You can use this and you will get desired output.
BigDecimal endDte = new BigDecimal("2609.8200");
BigDecimal startDte = new BigDecimal("100");
BigDecimal finalPerformance = endDte.divide(startDte);
System.out.printf("%2.6f%n", finalPerformance);
Other option if you always want to divide by 100, you can just shift the decimal. When you do that, the precision remains the same. In that case the new code to try is
BigDecimal endDte = new BigDecimal("2609.8200");
//BigDecimal startDte = new BigDecimal("100");
BigDecimal finalPerformance = endDte.movePointLeft(2);
System.out.println(finalPerformance);
I have a BigDecimal value, for example
BigDecimal bdVal = new BigDecimal("3.141592653");
I really want this value printed to be
dbVal: 3.141600000
What should I do for that ???
If you would like to round the value up to four decimal places, use
BigDecimal rounded = bdVal.setScale(4, RoundingMode.HALF_UP);
To print it with nine zeros, use this format:
DecimalFormat decFormat = new DecimalFormat("0.000000000");
String formatted = decFormat.format(rounded);
Demo.
I'm trying divide a value using BigDecimal, when this value is a decimates BigDecimal round this value and I wont to do that. I need the decimates value are shown. For example, if I do divide 10 / 3 = 3.33333, I need shown 3.33 but does show 3.00
How could I do this ?
//Result-> 10 / 2 = 3,3333333
BigDecimal result = new BigDecimal(0);
BigDecimal v1 = new BigDecimal(10);
BigDecimal v2 = new BigDecimal(3);
result = v1.divide(v2, BigDecimal.ROUND_UP);
//output = 3
//I need output = 3.33
The scale of BigDecimals that were initialized with ints is 0, meaning that rounding operations round to unity. With your example, the division rounds up to 4.
Set the scale of the first BigDecimal to 2, which be retained through the division. Also set the rounding mode to "down".
BigDecimal v1 = new BigDecimal(10).setScale(2);
BigDecimal v2 = new BigDecimal(3);
result = v1.divide(v2, BigDecimal.ROUND_DOWN);
Printing results now yields an output of 3.33.
You could have also used the RoundingMode enum as a drop-in replacement for the rounding constants. Additionally, you could have used ROUND_HALF_DOWN, ROUND_HALF_UP, or ROUND_HALF_EVEN (or their RoundingMode equivalents).
You could use a string with the appropriate number of decimal digits to set the scale implicitly.
BigDecimal v1 = new BigDecimal("10.00"); // scale of 2
try to compile and run this java class, it works as you wish:
import java.math.*;
class decimal {
public static void main(String[] args) {
BigDecimal result = new BigDecimal(0);
BigDecimal v1 = new BigDecimal(10);
BigDecimal v2 = new BigDecimal(3);
result = v1.divide(v2,2,BigDecimal.ROUND_HALF_UP);
System.out.println(result);
}
}
output : 3.33
you want two decimal digits this is why I set scale=2
Try to add scale to BigDecimal, like this:
public class User {
public static void main(String[] args) {
BigDecimal result = new BigDecimal(0);
BigDecimal v1 = new BigDecimal(10).setScale(2);
BigDecimal v2 = new BigDecimal(3);
result = v1.divide(v2, BigDecimal.ROUND_DOWN);
System.out.println(result);
}
}
I have tried the following code but it is not working in a particular case.
Eg: Suppose, I have a double value=2.5045 and i want it to be rounded off upto two decimal places using the below code.After rounding off, i get the answer as 2.5. But I want the answer to be 2.50 instead. In this case,zero is trimmed off. Is there any way to retain the zero so as to get the desired answer as 2.50 after rounding off.
private static DecimalFormat twoDForm = new DecimalFormat("#.##");
public static double roundTwoDecimals(double amount) {
return Double.valueOf(twoDForm.format(amount));
}
try this pattern
new DecimalFormat("0.00");
but this will change only formatting, double cannot hold number of digits after decimal poin, try BigDecimal
BigDecimal bd = new BigDecimal(2.5045).setScale(2, RoundingMode.HALF_UP);
Look at the documentation for DecimalFormat. For # it says:
Digit, zero shows as absent
0 is probably what you want:
Digit
So what you are looking for is either "0.00" or "#.00" as a format string, depending on whether you want the first digit before the period, to be visible in case the numbers absolute value is smalle than 0.
Try this
DecimalFormat format = new DecimalFormat("#");
format.setMinimumFractionDigits(2);
answer.setText(format.format(data2));
Try This
double d = 4.85999999999;
long l = (int)Math.round(d * 100); // truncates
d = l / 100.0;
You are returning a double. But double or Double are objects representing a number and don't carry any formatting information. Ìf you need to output two decimal places the point to do this is when you convert your double to a String.
use # if you want to ignore 0
new DecimalFormat("###,#0.00").format(d)
There is another way to achieve this . I have already posted answer in post
will just answer again here. As we will require rounding off values many times .
public class RoundingNumbers {
public static void main(String args[]){
double number = 2.5045;
int decimalsToConsider = 2;
BigDecimal bigDecimal = new BigDecimal(number);
BigDecimal roundedWithScale = bigDecimal.setScale(decimalsToConsider, BigDecimal.ROUND_HALF_UP);
System.out.println("Rounded value with setting scale = "+roundedWithScale);
bigDecimal = new BigDecimal(number);
BigDecimal roundedValueWithDivideLogic = bigDecimal.divide(BigDecimal.ONE,decimalsToConsider,BigDecimal.ROUND_HALF_UP);
System.out.println("Rounded value with Dividing by one = "+roundedValueWithDivideLogic);
}
}
Output we will get is
Rounded value with setting scale = 2.50
Rounded value with Dividing by one = 2.50
double kilobytes = 1205.6358;
double newKB = Math.round(kilobytes*100.0)/100.0;
DecimalFormat df = new DecimalFormat("###.##");
System.out.println("kilobytes (DecimalFormat) : " + df.format(kilobytes));
Try this if u are still getting the above problem