Reputation: 85
In the program I am writing I have a percent change function. With the numbers I'm using, it is supposed to return .03, yet I just get 0. Using a calculator, the value not formatted would be 0.03303. I want the number to be truncated after the second decimal place, no rounding at all.
df = new DecimalFormat("###.##;-###.##");
df.setRoundingMode(RoundingMode.DOWN);
//later in code
return df.format(((price-base_price)/price)*100.00D);
Edit: all variable types are doubles. Base price is 756.60 and price is 756.85
Upvotes: 1
Views: 417
Reputation: 201527
Your assumption about the value calculated in ((price-base_price)/price)*100.00D
is incorrect (I think you've probably invoked integer math, try ((price-base_price)/(double)price)*100.00D
). When I directly execute,
double value = 0.03303;
DecimalFormat df = new DecimalFormat("###.##;-###.##");
df.setRoundingMode(RoundingMode.DOWN);
System.out.println(df.format(value));
I get the (requested) output
0.03
Upvotes: 4