Can anyone give me an easy way to find out the numbers after the decimal point of the double?
All i need to do is to find out if the number ends in 0 like 1.0 or 10.0 or 32
Do you mean something like:
if (x == Math.Floor(x))
? Note that usually with binary floating point arithmetic, you want to have some sort of tolerance, so that 10.000001 was treated as "pretty close to 10". For example:
if (x - Math.Floor(x) < Tolerance)
{
...
}
What you need to do is multiply by 10 and then mod 10.
For instance. lets say x = 10.2
temp = x * 10 ' temp equals 102
temp = temp % 10 ' that should return the 2. Temp is now the value of the decimal you need.
This should work for negative values as well.
If you need to compare to see if it is a 0, all you do is compare your temp var to 0.
Try this function:
Function CalculateDecimals(input As Double)
Dim positiveInput As Double
positiveInput = Math.Abs(input)
Return positiveInput - Math.Floor(positiveInput)
End Function
It will give you the decimals you are looking for.
Use it in this way:
If (Math.Abs(CalculateDecimals(yourNumber))<0.00001) Then ...
If you are using any of the numeric data types (e.g. double, single, integer, decimal), you cannot represent 10.0 and 10 as two different and distinct values. There is no way to do that. If you need to keep the mantissa of the number, even when it is zero, you will have to store the value as some other data type such as a string.
To validate a string that has been entered, you could do something like this:
Public Function MantissaIsZero(ByVal value As String) As Boolean
Dim result As Boolean = False
Dim parts() As String = value.Split("."c)
If parts.Length = 2 Then
Dim mantissa As Integer = 0
If Integer.TryParse(parts(1), mantissa) Then
result = (mantissa = 0)
End If
End If
Return result
End Function
Just use the Mod
operator, like so:
If (x Mod 1) = 0 Then
' Do some stuff
End If
(see http://en.wikibooks.org/wiki/Visual_Basic_.NET/Arithmetic_operators)
Here is something you can try. Converting the double to an int removes the decimal place. Then you can do a Mod operation on the same number get the remainder equal to what is in the decimal place.
(double)(4.9 % (int)(4.9))