title: Converting double to NSDecimalNumber while maintaining accuracy
body: <p>I need to convert the results of calculations performed in a <code>double</code>, but I cannot use <code>decimalNumberByMultiplyingBy</code> or any other <code>NSDecimalNumber</code> function. I've tried to get an accurate result in the following ways:</p>
  
  <pre><code>double calc1 = 23.5 * 45.6 * 52.7;  // &lt;-- Correct answer is 56473.32
  NSLog(@"calc1 = %.20f", calc1);
  </code></pre>
  
  <p><strong>-> calc1 = 56473.32000000000698491931</strong></p>
  
  <pre><code>NSDecimalNumber *calcDN = (NSDecimalNumber *)[NSDecimalNumber numberWithDouble:calc1];
  NSLog(@"calcDN = %@", [calcDN stringValue]);
  </code></pre>
  
  <p><strong>-> calcDN = 56473.32000000001024</strong></p>
  
  <pre><code>NSDecimalNumber *testDN = [[[NSDecimalNumber decimalNumberWithString:@"23.5"] decimalNumberByMultiplyingBy:[NSDecimalNumber decimalNumberWithString:@"45.6"]] decimalNumberByMultiplyingBy:[NSDecimalNumber decimalNumberWithString:@"52.7"]];
  NSLog(@"testDN = %@", [testDN stringValue]);
  </code></pre>
  
  <p><strong>-> testDN = 56473.32</strong></p>
  
  <p>I understand that this difference is related to the respective accuracies.  </p>
  
  <p>But here's my question: How can I round this number in the most accurate way possible regardless of what the initial value of <code>double</code> may be? And if a more accurate method exists to do the initial calculation, what is that method?</p>