In Objective-C, I used this code to format a value so that a value with zero decimal places will be written without decimal places, and a value with decimal places will be written with one decimal place:
CGFloat value = 1.5; return [NSString stringWithFormat:@"%.*f",(value != floor(value)),value];
I need to do the same for a double value in Java, I tried the following, but this does not work:
return String.format("%.*f",(value != Math.floor(value)),value);
source share