0

Why does the %g format for strings only handle six numbers in a float and after that it turns into scientific notation? Is there any other way of displaying a float with something similar to the %g format but allows more than six numbers?

EDIT: I have figured out %g with precision i.e turning %g into %.Xg where x is the specified number of significant digits. But it doesnt help me in this situation:

-(IBAction)numberPressed:(id)sender {
    if (decimalChecker == 1) {

            currentDecimal = currentDecimal*10+ (float)[sender tag];
            decimaledNumberString = [[NSString alloc] initWithFormat:@"%.17g.%.17g", currentNumber, currentDecimal];
        calculatorScreen.text = decimaledNumberString;
        currentDecimaledNumber = [decimaledNumberString floatValue];
        NSLog(@"regular");
            } else {

                    currentNumber = currentNumber*10+ (float)[sender tag];
            calculatorScreen.text = [[NSString alloc] initWithFormat:@"%.17g", currentNumber];
            NSLog(@"regular");
    }
}

If I press "5" eight times instead of 55555555, I get 55551782 or something similar. How can I fix it to where I get the desired eight fives instead of the crazy number?

Milo
  • 5,041
  • 7
  • 33
  • 59
  • 1
    see http://stackoverflow.com/questions/5913102/what-is-the-difference-between-g-and-f-in-c – Till Jun 18 '13 at 19:15
  • Thanks but this still doesn't solve my problem. I still need to display more than 6 numbers. Is there any way to remove the trailing zeros with the `%f` format specifier? – Milo Jun 18 '13 at 19:24
  • indeed that comment was mostly intended for the already removed comment by JAB. However, that answer was linking the printf man page which you should really consult instead of asking here. – Till Jun 18 '13 at 19:28

2 Answers2

2

Insert a period and a numeral to specify the maximum number of significant digits you would like displayed, such as %.17g for 17 significant digits. As you discovered, the default is six.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • This works in some cases for me but when using that in this code after six digits it starts rounding or doing something crazy. Instead of 7777777777, I get 7777731283. http://pastebin.com/7eWJ1Vga – Milo Jun 18 '13 at 19:36
  • @user1597119 you should really learn how floating point numbers work... I highly suggest you to read and understand http://en.wikipedia.org/wiki/Floating_point – Till Jun 18 '13 at 19:39
  • @user1597119: That is likely a separate issue from formatting a number from display. The `float` format has only 24 bits for the significand of a number, so the value cannot be represented exactly beyond about the seventh decimal digit. You can use `double` for more precision, and you can study floating-point arithmetic to learn how to better control its approximations. – Eric Postpischil Jun 18 '13 at 19:40
  • Thanks Eric, I've switched over to doubles and now everything is working right! – Milo Jun 18 '13 at 19:47
1

According to http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Strings/Articles/FormatStrings.html#//apple_ref/doc/uid/20000943, iOS string formatting uses the same placeholders as C's printf(), which specifies g/G as representing FP values with exponential notation for very large/small values while f only uses non-exponential representation.

http://en.wikipedia.org/wiki/Printf_format_string#Format_placeholders

JAB
  • 20,783
  • 6
  • 71
  • 80
  • 1
    The correct answer would have been "use precision - e.g. `%.9g` which would result into up to 9 values in precision`. – Till Jun 18 '13 at 19:20