I have a variable that sometimes is a whole number and sometimes has a decimal. When it’s a whole number, I’d like to show it WITHOUT a decimal and when it’s not, I’d like to show the decimal.
Example: variable is 16.00 and displays as 16%
Example: variable is 16.50 and displays as 16.50%
Here’s what I wrote AND IT WORKS using if/else…
BUT it seems convoluted and I’m wondering if there’s an easy way that I’m missing.
Thanks in advance for any help you can give.
int intTotalLabelData = [totalLabelData floatValue];
if ([totalLabelData floatValue] == intTotalLabelData) {
totalLabel.text = [NSString stringWithFormat:@"%.0f%%", [totalLabelData floatValue]];
[self calculateMethod];
}
else {
totalLabel.text = [NSString stringWithFormat:@"%.1f%%", [totalLabelData floatValue]];
[self calculateMethod];
}
Use g instead of f to format your float to a string. This will give you “16%” for 16.00 and “16.5%” for 16.50.