I have variables that are doubles and I want to get the double with only 2 numbers after the decimal point. I tried it with
System.out.printf("%1.2f", number1);
but the decimal places get rounded. For example if I have number1 = 19.867, I just want to get 19.86 and not the rounded version 19.87. How can I do that?
You can use DecimalFormat with an apropriate pattern and a RoundingMode to specify rounding behavior:
double number1 = 19.867;
DecimalFormat df = new DecimalFormat("#.##");
df.setRoundingMode(RoundingMode.DOWN);
System.out.println(df.format(number1));
The same can be achieved by converting the double value to BigDecimal and use BigDecimal‘s setScale() method with a RoundingMode
BigDecimal bigDecimalNumber1 = new BigDecimal(number1).setScale(2, RoundingMode.DOWN);
System.out.println(bigDecimalNumber1.doubleValue());
Related
I have a number with some number decimal places,
How can i round this float number with one number decimal places
for example
1.366565646 convert to 1.3
In your case I think you need to trim the number not round it,
You can use this:
double d = 1.366565646;
DecimalFormat df = new DecimalFormat("#.#");
double p = Double.parseDouble(df.format(d));
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
When I click calculate to display a calculation it displays the following answer:
Net Pay 31030.38076923077
How can I make it only display two decimal numbers after the dot. example $31030.38
Use DecimalFormat:
double f = 31030.38076923077;
DecimalFormat df = new DecimalFormat("#.00");
System.out.println(f+" "+df.format(f));
Use BigDecimal like this
double d = 31030.38474923077;
BigDecimal dec = new BigDecimal(d).setScale(2, RoundingMode.HALF_UP);
System.out.println(dec);
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
I have a number
double num = 1.234567;
and I'm trying to keep only two decimals
num = (int)((num * 100) + 0.5) / 100.0;
but the actual number I got is 1.230000000001. How can I get rid of the 0000000001 part?
Try DecimalFormat:
DecimalFormat twoDp= new DecimalFormat("#.##");
Double.valueOf(twoDp.format(num));
You can't, unless you switch to a decimal radix. Doubles and floats don't have decimal places, they have binary places, so you can't round or truncate them to specific numbers of decimal places except in the cases where the value representations are congruent, i.e. the negative powers of 2.
So you have to either use DecimalFormat if you are presenting the result, or BigDecimal if you want to keep computing with it.
Any solution that ends by turning the value back into floating point is incorrect.
String result = String.format("%.2f", num);
Try this .. this should solve it
Double num = //value
num = //arithmetic
String temp =num.toString().split("\\.")[0];
int precision=temp.length();
BigDecimal b =new BigDecimal(num,new MathContext(precision+2));
System.out.println(b.doubleValue());