0

I can't achieve rounding a float.

Those calls all returns me 3.5999999 and not 3.6 for theoTimeoutTrick and theoTimeout.
How may I achieve to get that 3.6 value, into NSString AND float vars ?

#define minTimeout 1.0
#define defaultNbRetry 5

    float secondsWaitedForAnswer = 20.0;
    float minDelayBetween2Retry = 0.5;

    int theoNbRetries = defaultNbRetry;
    float theoTimeout = 0.0;

    while (theoTimeout < minTimeout && theoNbRetries > 0) {
        theoTimeout = (secondsWaitedForAnswer - (theoNbRetries-1)*minDelayBetween2Retry) / theoNbRetries;
        theoNbRetries--;
    }

    float theoTimeoutTrick = [[NSString stringWithFormat:@"%.1f", theoTimeout] floatValue];
    theoTimeout = roundf(theoTimeout * 10)/10.0;
mskfisher
  • 3,291
  • 4
  • 35
  • 48
Oliver
  • 23,072
  • 33
  • 138
  • 230
  • 3.6 is not precisely representable in binary with finitely many digits. – Kerrek SB Jul 25 '11 at 00:12
  • @Kerrek SB : I can understand but I don't care. I have to display 3.6 and that's the correct rounded value. Have you ever seen such decimals on your banking account for example ? – Oliver Jul 25 '11 at 00:14
  • 6
    I'm sure my banking account doesn't use IEEE floats for my balance! (Mainly because of their limited range :-) ). Use formatted output to print a suitably rounded value. – Kerrek SB Jul 25 '11 at 00:17
  • @Kerrek SB : What do you think about when talking about a formatted output ? stringWithFormat does not do the job ? – Oliver Jul 25 '11 at 00:18
  • 1
    You format when you actually output to a string. All I can see you do is assign floats and ints. Where's your output printing? – Kerrek SB Jul 25 '11 at 00:20
  • 3
    Alternatively, use NSDecimal and NSDecimalNumber. – tc. Jul 25 '11 at 00:23
  • 2
    @Kerrek money handling software usually uses binary encoded decimals (BED), which use 4 bits for every digit. However, if you try something like `NSLog(@"%f", 3.6);` you will find that it does indeed print `3.60000` –  Jul 25 '11 at 00:26
  • @bdares: Yeah, I'm sure they use some suitable decimal format. It'd be very unfortunate otherwise! I think there's also some decimal float support in C99, but I don't know about ObjC. – Kerrek SB Jul 25 '11 at 00:44
  • @Kerrek SB: Objective-C is a superset of C, so if you tell the compiler to use C99, that will apply to Objective-C as well as to pure C. – Peter Hosey Jul 25 '11 at 05:18

1 Answers1

3

From Rounding numbers in Objective-C:

float roundedValue = round(2.0f * number) / 2.0f;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:1];
[formatter setRoundingMode: NSNumberFormatterRoundDown];

NSString *numberString = [formatter stringFromNumber:[NSNumber numberWithFloat:roundedValue]];
[formatter release];

That will get you a rounded string. You can parse it back into a float I'm sure.

Okay, here's some test code and some results:

NSLog(@"%f", round(10*3.56)/10.0);
    =>3.600000
NSLog(@"%f", round(10*3.54)/10.0);
    =>3.500000
NSLog(@"%f", round(10*3.14)/10.0);
    =>3.100000

Okay, you know what? Your original code works as intended on my machine, OSX 10.6 and Xcode 4. How exactly are you seeing your output?

Community
  • 1
  • 1
  • @Oliver: The question for that post was asking for rounding to nearest 0.5, so he rounded after multiplying by 2, then divided result by 2. You can use `[formatter setMaximumFractionDigits:0]` if you want to round to the nearest whole number. –  Jul 25 '11 at 00:32
  • Oucchhhh... I mean... Ouucchhh ! What the f.... Well that works not so good because of the round type. I just get 3.5. Two questions : what is the use of 2.0f here ? What NSNumberFormatter constant may I use to round the decimal with the common method (you know until 0.5 it rounds down, and with 0.5 and more it rounds up) ? – Oliver Jul 25 '11 at 00:35
  • If you'd take five seconds to search the docs for `NSNumberFormatterRoundDown` you'd find the answer… – Rob Keniger Jul 25 '11 at 00:35
  • @bdares : I just want here to get 3.6 – Oliver Jul 25 '11 at 00:35
  • @Rob Keniger : Ah ah ah. Does not work. Of course I read the doc. If I set float roundedValue = 3.56; I just get 3.5, where I should have 3.6. – Oliver Jul 25 '11 at 00:38
  • @Oliver you really should think a bit more about it. If the original guy could round to the nearest (not floor) 0.5, then surely you can modify his code to get the nearest 0.1. Try multiplying by 10, rounding, then dividing by 10 before you take the floor? –  Jul 25 '11 at 00:52
  • That does N.O.T work. Why do you think I'm asking ? Did you try ? I just keep getting some 3.5 or 3.5999999999 values. – Oliver Jul 25 '11 at 00:55
  • @bdares : I'm looking at the output through the debugger – Oliver Jul 25 '11 at 20:45
  • @Oliver I was looking at it through my debugger as well (otherwise I wouldn't see the `NSLog()` output). You might want to update your sdk. –  Jul 26 '11 at 04:22
  • @bdares : uhhh, it's up to date. I checked the update two days ago. – Oliver Jul 26 '11 at 17:14