2

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];
}
Jupiter869
  • 657
  • 2
  • 7
  • 19
  • 1
    possible duplicate of [Remove More Than 2 Trailing zero](http://stackoverflow.com/questions/7469614/remove-more-than-2-trailing-zero) - there are some ideas in the answers there, and some links to other SO questions that may help you. – jrturton Jan 09 '12 at 17:27

4 Answers4

4

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.

totalLabel.text = [NSString stringWithFormat:@"%.1g%%", [totalLabelData floatValue]];
bbarnhart
  • 6,620
  • 1
  • 40
  • 60
  • 1
    Thanks. I didnt realize about using g. However, it gave me 2e+01 as a result. I must be doing something wrong. – Jupiter869 Jan 09 '12 at 21:16
  • You can try it with a primitive type such as a double or float to see that it works and this is what you want. Make sure totalLabelData is not nil. Also, check that totalLabel is not nil as well. – bbarnhart Jan 10 '12 at 03:06
1

I would have done it like this:

float number;
double integerPart;
double fractionPart;
fractionPart = modf(number, &integerPart);
if (fractionPart == 0) NSLog(@"%.0f", integerPart); 
else NSLog(@"%.2f", integerPart + fractionPart);

In case you don't know, modf(double, double *) returns the integer part of the given number and sends the fraction part to the address given as a 2 argument. If you like floats more, use modff(float, float *)

Vladislav Zakatov
  • 146
  • 1
  • 1
  • 6
0

Your appraoch to the problems seems completely fine. However if you would like to achieve the same results with fewer number of lines, then please look at the example below.

int intTotalLabelData = [totalLabelData floatValue];
totalLabel.text = [NSString stringWithString: intTotalLabelData == [totalLabelData floatValue]? [NSString stringWithFormat:@"%i%%",intTotalLabelData] : [NSString stringWithFormat:@"%.1f%%", [totalLabelData floatValue]]];
[self calculateMethod];

Note 1: To format a string with an integer (number without decimals) you must use %i.

Note 2: My code is in no way more efficient than your code; the compiler will take relatively the same time to compile both pieces of code. However it is a little bit shorter...I guess.

Happy Coding.

JustKash
  • 687
  • 2
  • 13
  • 28
  • Wow, I love efficient code like this! I didn't realize you could do something like this. Unfortunately though, I get an incompatible operand types 'float' and 'NSString' error when I try to use it. – Jupiter869 Jan 09 '12 at 21:58
0

see NSNumberFormatter or CFNumberFormatter

justin
  • 104,054
  • 14
  • 179
  • 226