Reputation: 1473
Following is the Java code to check if the number is whole number or decimal number and display the result accordingly:
if (result % 1 == 0) {
String finalResultString = String.format("%.0f", result);
} else {
BigDecimal decimalResult = new BigDecimal(result);
String resultString = String.format("%.15f", decimalResult);
}
But in the decimal part:
1) If I run 66%6, I get 3.960000000000000 but I want the answer to be 3.96 only. So I do not want the unnecessary zeros to be displayed.
2) If I run 99 x 65.00055, I want the answer to be 15 digits in after decimal place like 6435.054450000000543
So the thing is - I don't want zeros depending on the leading digit. Like if the result is 5.645345600000000 or 3.960000000000000, I want the result to be either 5.6453456 or 3.96. If the result is 6435.054450000000540, I want 14 digits of precision in the result to be shown since i don't want the last unnecessary zero to be shown. If the result is 6435.000000000000003, I want all the 15 digits of precision to be shown since there are no unnecessary zeros at the end
Can anyone please help?
Thanks in advance
Upvotes: 0
Views: 167
Reputation: 600
You can set the decimal scale to 2:
decimalResult = decimalResult.setScale(2);
Upvotes: 0