Android Question “Solved” How to get the decimal part of a number?

Sergey_New

Well-Known Member
Licensed User
Longtime User
B4X:
Dim latitude As Double=43.123456
Dim m As Long
'...
' need to get the value m=123456
How to do it?
 

JohnC

Expert
Licensed User
Longtime User
ChatGPT says...

To extract the decimal part of a number in B4A, you can use some basic mathematical operations and string manipulations. Here’s how you can achieve it:
  1. Convert the number to a string.
  2. Find the position of the decimal point.
  3. Extract the substring starting from the position of the decimal point + 1.
  4. Convert this substring back to a number if needed.
Here is the code that demonstrates this approach:
B4X:
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)
This will give you m = 123456 as desired.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Or short version
B4X:
    Dim num As Double = 43.123456
    Dim dec As Int = num.As(String).SubString((" " & num).IndexOf("."))
    Log(dec)

I probably should explain why I don't need position of "." + 1
The num string is
"43.123456"

The string I create to find the dot is
" 43.123456" <-- has a prefixed space

When I find the position of the dot, and use on the original string, it's actually pointing at the character after the dot.

Hence no need for + 1 in the substring function.
 
Last edited:
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
JohnC, thank you!
This decision is clear to me. I was hoping that there was a special function in B4A.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
Daestrum, thank you!
Tell me if your solution will work if there is no "." in the number?
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
ChatGPT says...
Checked if the number is 43.0 or 43
Changed the code
B4X:
    If decimalPosition=-1 Then
        m=0
    Else
        Dim decimalPart As String = latitudeString.SubString(decimalPosition + 1)
        m = decimalPart       
    End If
This is right?
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Checked if the number is 43.0 or 43
Changed the code
B4X:
    If decimalPosition=-1 Then
        m=0
    Else
        Dim decimalPart As String = latitudeString.SubString(decimalPosition + 1)
        m = decimalPart      
    End If
This is right?
Looks correct
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Continuing with @Daestrum solution
B4X:
Dim num As Double = 43
If num.As(String).Contains(".") Then
    Dim dec As Int = num.As(String).SubString((" " & num).IndexOf("."))
Else
    Dim dec As Int = 0
End If
Log(dec)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
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)
 
Upvote 1

Sergey_New

Well-Known Member
Licensed User
Longtime User
Thanks to everyone who responded!
The proposed solutions work.
Therefore, I will mark “Solved” in the topic title.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Thanks to everyone who responded!
The proposed solutions work.
Therefore, I will mark “Solved” in the topic title.
You can mark also as "Solution" the one you "prefer"

1717006464696.png
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Just for fun this works with no "." and negative numbers
B4X:
    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)

It does produce some spooky java source code lol
B4X:
_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))))))))));
 
Last edited:
Upvote 0
Top