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
Dim latitude As Double = 43.123456
Dim m As Long
' Convert the number to a string
Dim latitudeString As String = latitude
' Find the position of the decimal point
Dim decimalPosition As Int = latitudeString.IndexOf(".")
' Extract the decimal part as a substring
Dim decimalPart As String = latitudeString.SubString(decimalPosition + 1)
' Convert the decimal part to a long number
m = decimalPart
Log("Decimal part: " & m)
Dim num As Double = 43.123456
Dim dec As Int = num.As(String).SubString((" " & num).IndexOf("."))
Log(dec)
Checked if the number is 43.0 or 43ChatGPT says...
If decimalPosition=-1 Then
m=0
Else
Dim decimalPart As String = latitudeString.SubString(decimalPosition + 1)
m = decimalPart
End If
no it won't as it looks for "."Tell me if your solution will work if there is no "." in the number?
Looks correctChecked if the number is 43.0 or 43
Changed the code
This is right?B4X:If decimalPosition=-1 Then m=0 Else Dim decimalPart As String = latitudeString.SubString(decimalPosition + 1) m = decimalPart End If
Dim pVal As Double = 43.123456
Dim pDec As Double = pVal - Floor(pVal)
Log(pDec)
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)
You can mark also as "Solution" the one you "prefer"Thanks to everyone who responded!
The proposed solutions work.
Therefore, I will mark “Solved” in the topic title.
Dim num As Double = -43.123456
Dim dec As Int = IIf((""&num).indexof(".")=-1,0,num.As(String).SubString((" " & num).IndexOf("."))*IIf(num<0,-1,1))
Log(dec)
_dec = (int)(BA.ObjectToNumber((((""+BA.NumberToString(_num)).indexOf(".")==-1) ? ((Object)(0)) : ((Object)((double)(Double.parseDouble((BA.NumberToString(_num)).substring((" "+BA.NumberToString(_num)).indexOf("."))))*(double)(BA.ObjectToNumber(((_num<0) ? ((Object)(-1)) : ((Object)(1))))))))));
Dim latitude As Double= 43.123456
Dim m As String = Regex.Split("\.", latitude)(1)
Log(m)
It would crash if latitude had no decimals.B4X:Dim latitude As Double= 43.123456 Dim m As String = Regex.Split("\.", latitude)(1) Log(m)
This seems to work, but I don't know if it's correct.
I have tested it in B4J and it does not crash but it shows thisIt would crash if latitude had no decimals.