title: Formatting a String of a double with swift
body: <p>How can i format string of a double with swift?
  I'm making a calculator like the basic calculator on iOS and i need a solution that change the displayed result depending on how many characters are after the dot (not exceeding 9 digits total). </p>
  
  <pre><code>Example Table
  |result   | wished displayed result | current displayed result
  |         |                         |
  |1.034    | 1.034                   | 1.03
  |1.000567 | 1.000567                | 1.00
  |1.2      | 1.2                     | 1.20
  </code></pre>
  
  <p>Using this code: </p>
  
  <p><code>result.text = String(format: "%.2f", displayNumber)</code> </p>
  
  <p>if i get 1.000334 as a result it only show 1.00</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I forgot to say that i need to not exceed the maximum of 9 characters including the point. If i remove <code>%.2f</code> i do not get the desired result because if i get 1.323429249430003442 as a result the displayed result will be 1.323429249430003442 with many numbers (must not exceed 9 digits including the point), otherwise adding <code>%.9f</code> is not good because if i get 1.32 as a result the displayed result will be 1.32000000 with a lot of unnecessary zeros.</p>
  
  <p>Thank you all in advance</p>
  
  <p><strong>EDIT 2 (SOLVED):</strong></p>
  
  <p>if someone would serve in the future, I solved it this way:</p>
  
  <pre><code>var decimalNumberAsString = "\(resultNumber - floor(resultNumber))"
  var decimalTrim: NSArray = decimalNumberAsString.componentsSeparatedByString(".")
  numberOfDecimalPlaces = decimalTrim.objectAtIndex(1).length
  if numberOfDecimalPlaces &lt; 8 {
    numberOfDecimalPlaces = decimalTrim.objectAtIndex(1).length + 1
  }
  else {
    numberOfDecimalPlaces = 7
  }
  result.text = String(format: "%.\(numberOfDecimalPlaces)G", resultNumber)
  </code></pre>