padvou Active Member Licensed User Longtime User Nov 10, 2020 #1 Hi! Consider this string: 637406139915958403 and this: 637406139915958402 using this sub: B4X: Sub CI(str As String) As Long If IsNumber(str) Then Try Dim ret As Long ret = str Return ret Catch Return 0 End Try Else Return 0 End If End Sub returns 637406139915958400 in both strings. Why??
Hi! Consider this string: 637406139915958403 and this: 637406139915958402 using this sub: B4X: Sub CI(str As String) As Long If IsNumber(str) Then Try Dim ret As Long ret = str Return ret Catch Return 0 End Try Else Return 0 End If End Sub returns 637406139915958400 in both strings. Why??
OliverA Expert Licensed User Longtime User Nov 10, 2020 #2 Almost looks like the values are first converted to a Double and then to an Int a Long Code: Dim one As String = "637406139915958402" Dim two As String = "637406139915958403" Dim double1 As Double = one Dim double2 As Double = two Log($"$18{double1}"$) Log($"$18{double2}"$) Output: double1: 637,406,139,915,958,400 double2: 637,406,139,915,958,400 Click to expand... Upvote 0
Almost looks like the values are first converted to a Double and then to an Int a Long Code: Dim one As String = "637406139915958402" Dim two As String = "637406139915958403" Dim double1 As Double = one Dim double2 As Double = two Log($"$18{double1}"$) Log($"$18{double2}"$) Output: double1: 637,406,139,915,958,400 double2: 637,406,139,915,958,400 Click to expand...
OliverA Expert Licensed User Longtime User Nov 10, 2020 #3 Confirmed Code: Dim one As String = "637406139915958402" Dim long1 As Long = one The resulting Java code fo the long1 assignment is Java: _long1 = (long)(Double.parseDouble(_one)); Upvote 0
Confirmed Code: Dim one As String = "637406139915958402" Dim long1 As Long = one The resulting Java code fo the long1 assignment is Java: _long1 = (long)(Double.parseDouble(_one));
OliverA Expert Licensed User Longtime User Nov 10, 2020 #4 A solution: Use @agraham's BigNumbers library (https://www.b4x.com/android/forum/threads/bignumbers-library.9540/#content): Code: Dim one As String = "637406139915958402" Dim bigN As BigInteger bigN.Initialize(one) Dim long1 As Long = bigN.LongValue Log($"long1: ${long1}"$) Result: long1: 637406139915958402 Click to expand... Upvote 0
A solution: Use @agraham's BigNumbers library (https://www.b4x.com/android/forum/threads/bignumbers-library.9540/#content): Code: Dim one As String = "637406139915958402" Dim bigN As BigInteger bigN.Initialize(one) Dim long1 As Long = bigN.LongValue Log($"long1: ${long1}"$) Result: long1: 637406139915958402 Click to expand...