title: Fractional Part of Doubles without using string manipulation
body: <p>Is there a way to get the fraction parts of a Double without using string manipulations? Specifically in vb.net?</p>
  
  <p>There are numerous examples for a decimal in various languages. <a href="https://stackoverflow.com/questions/362441/split-a-decimal-in-vb-net">One for vb.net is this one</a> </p>
  
  <p>Basically they all seem to use either a mod or Truncate method or they take advantage of casting to integer behavior. But none of those approaches will work for a double/float type due to the inherent inaccuracy of the double/float type. There is also the problem that doubles don't cast to decimals reliably. Below is a test case to show what I mean and expect.</p>
  
  <pre><code>&lt;TestClass&gt; _
      Public Class BasicNumberTests
  
          Function DecimalFractionalPart(ByVal number As Decimal) As Decimal
              Dim wholePart As Decimal = Math.Truncate(number)
              Return number - wholePart
          End Function
  
          Function DoubleFractionalPart(ByVal number As Double) As Double
              Dim wholePart As Double = Math.Truncate(number)
              Return number - wholePart
          End Function
  
          &lt;TestMethod()&gt; Public Sub SplitDoubleAsDecimal1()
              Dim number As Double = 0.65 + 0.05
              Dim fractionalPart As Double = CDbl(DecimalFractionalPart(CDec(number)))
              Assert.AreEqual(0.70000000000000007, number)
              Assert.AreEqual(0.70000000000000007, fractionalPart) '&lt;- Fails
          End Sub
  
          &lt;TestMethod()&gt; Public Sub SplitDoubleAsDecimal2()
              Dim number As Double = 0.70000000000000007
              Dim fractionalPart As Double = CDbl(DecimalFractionalPart(CDec(number)))
  
              Assert.AreEqual(0.70000000000000007, number)
              Assert.AreEqual(0.70000000000000007, fractionalPart) '&lt;- Fails
          End Sub
  
          &lt;TestMethod()&gt; Public Sub SplitDoubleAsDouble1()
              Dim number As Double = 1.65
  
              Assert.AreEqual(1.65, number)
              Assert.AreEqual(0.65, DoubleFractionalPart(number)) '&lt;- Fails
          End Sub
  
          &lt;TestMethod()&gt; Public Sub SplitDoubleAsDouble2()
              Dim number As Double = 1.0 + 0.65
              Assert.AreEqual(1.65, number)
              Assert.AreEqual(0.65, DoubleFractionalPart(number)) '&lt;- Fails
          End Sub
  
  End Class
  </code></pre>
  
  <p>To get around this I have methods that use string manipulation to get the fractional part. The problem with this is that it only works with certain formatting types.</p>
  
  <p>I asked this question (incorrectly) in another place and (before my question was deleted) some person said it <em>would</em> work with doubles which I didn't understand.</p>
  
  <p>(To be clear I do realize that the Double you see isn't always exactly the actual number it appears to be. I know that .65 + .05 &lt;> .7  But I also know that 0.70000000000000007 is always = 0.70000000000000007 = .65 + .05 and so on. But I want the fractional part of the decimal AS IT APPEARS TO BE.) </p>
  
  <p>Am I missing something? Is there a way to get the fractional part of doubles reliably without using string manipulation?</p>
  
  <p>If I must use string manipulation how can I do this so that it will always work regardless of the cultural formatting?</p>
  
  <p>EDIT-The tests show what I want. If the Double is 0.70000000000000007 I want the method to return 0.70000000000000007.  If it is .65 I want .65 returned.  Since .65 + .05 = 0.70000000000000007 then GetFractionalParts(.65 + .05) should return 0.70000000000000007</p>