B4X:
Dim latitude As Double=43.123456
Dim m As Long
'...
' need to get the value m=123456
Dim latitude As Double=43.123456
Dim m As Long
'...
' need to get the value m=123456
Try:This is strange, try trying it with 43123456
Dim latitude As Double= 43123456
Log(latitude.As(String))
Thanks for your teachingTry:
B4X:Dim latitude As Double= 43123456 Log(latitude.As(String))
log:
4.3123456E7
Dim latitude As Double= 43.123456
Dim latitude As Double= 43
Dim latitude As Double= .123456
Dim m() As String = Regex.Split("\.", latitude)
Log(IIf(m.Length = 1, 0, m(1)))
Would this not work?B4X:Dim pVal As Double = 43.123456 Dim pDec As Double = pVal - Floor(pVal) Log(pDec)
Sub AppStart (Args() As String)
Log("Hello world!!!")
Private num As Double = 31.987654321
'inting
Log(num.As(Int))
Log(num-num.As(Int))
'flooring
Log(Floor(num))
Log(num-Floor(num))
End Sub
Hello world!!!
31
0.9876543210000008
31
0.9876543210000008
One of the problems with this approach is the rounding errors that can occur.
e.g.,
Example of Rounding Errors:Sub AppStart (Args() As String) Log("Hello world!!!") Private num As Double = 31.987654321 'inting Log(num.As(Int)) Log(num-num.As(Int)) 'flooring Log(Floor(num)) Log(num-Floor(num)) End Sub
resulting in
The best approach has to be converting it to a string first. That way, you avoid these slight rounding errors.
sorry but i dont understand.
you are dealing with doubles so why do you convert it to a int?
31
0.9876543210000008
31
0.9876543210000008
the best way to do it is just taking the double.
using floor to get the whole number before the decimal point and then substract the origin number with the whole number only.
To be honest, I didn't actually test this, that is why I phrased my reply as a question.sorry but i dont understand.
you are dealing with doubles so why do you convert it to a int?
the best way to do it is just taking the double.
using floor to get the whole number before the decimal point and then substract the origin number with the whole number only.
as @Jeffrey Cameron did.
if you conert to string and start spliting the string you may get errors like
if there is no point in the number or if the phone instead of dot use comma "," you may get a crash.
'VB.NET code
Dim pVal As Decimal = 43.123456
Dim pDec As Decimal = pVal - Math.Floor(pVal)
Debug.Print("Value is " & pDec)
'output shows "Value is 0.123456"
Dim pVal As Double = 43.123456789
Dim psVal As String = $"$0.9{pVal}"$
Log(psVal.SubString(psVal.IndexOf(".")+1))
you are right i see it now, it is really weirdThe original number is 31.987654321. Both int() and floor() achieve the same result... and both are wrong.
That's exactly what I showed in my example. And the result is wrong.
Sub DecimalPart ( dblstr As Double ) As String
If dblstr-Floor(dblstr) = 0 Then Return "0" Else Return Regex.Replace("^[\d].\.*",dblstr.As(String),"")
End Sub
Here the decimal part of a negative number results to be positive. This is incorrect.IndexOf(".") is not suitable, because you could have a different symbol as decimal separator (possibly you would have to use a variable in which to put the system separator).
The following code should work:
B4X:Dim Num As Double = 2.36 ' Dim Num As Double = -2.36 ' to test negative numbers Dim IntPart As Int Dim DecimalPart As Double If Num >= 0 Then IntPart = Floor(Num) Else IntPart = Ceil(Num) End If DecimalPart = Abs(Num - IntPart) DecimalPart = Round2(DecimalPart, 2) Log("Original number: " & Num) Log("Int part: " & IntPart) Log("decimal part: " & DecimalPart)
The decimal part is to be considered "absolute", without sign.Here the decimal part of a negative number results to be positive. This is incorrect.
-2 + 0.36 is not -2.36 but -1.64.
Incorrect if the number is negative.Looks correct
Not in math.The decimal part is to be considered "absolute", without sign.