2

I have an Objective-C project that needs to display numbers like 0.00000217, and very small numbers like that. Problem is, Objective-C is rounding this to the 6th decimal place so it displays as 0.000002. Is there a type to display more decimal places? My code:

float floatValueOfSmallNumber = [value floatValue];
[theLabel setText:[NSString stringWithFormat:@"%f", floatValueOfSmallNumber]];

Thanks!

Burrito411
  • 431
  • 1
  • 8
  • 15
  • This information can be found with a quick google search. For example, if I search for objective-c numeric types, the first result on that page indicates a `double` and what it is for. -1 for apparent lack of research effort. – borrrden Jan 28 '14 at 02:15
  • I looked for "float only 6 digits" and it would not return any results. – Burrito411 Jan 28 '14 at 02:15
  • Only attempting one google search AND not including the programming language (not that it's particularly relevant here, but it would produce better results) is still worthy of the "No research effort" downvote. – nhgrif Jan 28 '14 at 02:18
  • @Burrito411 I searched for that exact phrase and the second result mentioned doubles – borrrden Jan 28 '14 at 02:19
  • This question seems to meet the requirements. It has a clear title, background explanation and a description of any research tried, and correct use of English. Upvoted... http://meta.stackoverflow.com/help/quality-standards-error – Robert Karl Jan 28 '14 at 02:20
  • @RobertKarl thanks very much from a noob to you! – Burrito411 Jan 28 '14 at 02:22
  • Use `"%e"` for the format. – chux - Reinstate Monica Jan 28 '14 at 02:25
  • 3
    This question is *not* a duplicate of the suggested question; the questioner's problem is the format specifier he is using, not the precision of `float`. – Stephen Canon Jan 28 '14 at 02:26
  • @borrrden: simply using `double` does nothing to address the problem; this hardly seems to deserve a downvote. – Stephen Canon Jan 28 '14 at 02:42
  • @StephenCanon You are right, I should have paid better attention – borrrden Jan 28 '14 at 03:02

1 Answers1

10

While a float only has ~7 significant decimal digits, that's not the problem you are running up against here; 0.00000217 has only three significant digits, after all.

You are using the %f format specifier which is inherited from C and defined thus (7.21.6 Formatted input/output functions):

A double argument representing a floating-point number is converted to decimal notation in the style [−]ddd.ddd, where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it is taken as 6; if the precision is zero and the # flag is not specified, no decimal-point character appears. If a decimal-point character appears, at least one digit appears before it. The value is rounded to the appropriate number of digits.

Using double won't change this; instead, you need to change your format specifier. You can use %e or %g if you don't mind scientific notation, but another alternative would be to use a precision specifier: %.10f, for example, will print ten decimal digits.

Stephen Canon
  • 103,815
  • 19
  • 183
  • 269