Android Question Anyone can direct me to an integer formatting style: from 1250 to +1k

Hanz

Active Member
Hello,
Do we have a library which converts a number like 1250 into +1k? If none, any direction? Thanks!
 

Hanz

Active Member
Like this?
B4X:
If number > 1000 Then Return "+1k" Else Return number
no, something like dim s as string = some_library(1250) and returns +1K even the number is 1200, 1300, 1500, and so on... and if it is 2500 the return value is +2k
 
Upvote 0

MikeSW17

Active Member
Licensed User
The question is too vague, so has too many (probably incorrect) answers.
Do you simply want to truncate the least significant 3(?) digits? or round to nearest thousand?, or what?
 
Upvote 0

Hanz

Active Member
The question is too vague, so has too many (probably incorrect) answers.
Do you simply want to truncate the least significant 3(?) digits? or round to nearest thousand?, or what?
okay, I am learning the badger class by erel. But it can contain only about 2 or 3 numbers. Beyond it, you can no longer see the other digits. I'm thinking, instead of showing the entire numbers such as 1000 or 10000, I can show +1k only or +9k.
1626327674848.png
 
Upvote 0

pliroforikos

Active Member
Licensed User
You should continue the Select statements if you need larger numbers

B4X:
    Dim intA As Long = 4212357
    Dim myInt As Long = intA

    Dim sz As Int = 0
    Do While intA > 0
        intA = intA / 10
        sz = sz + 1
    Loop
    Dim msg As String
    Select sz
        Case 1,2,3
            Log(myInt)   
        Case 4,5,6
            myInt = myInt / Power(10, 3)
            msg = "+" & myInt & "K"
            Log(msg)
        Case 7,8,9
            myInt = myInt /  Power(10, 6)
            msg = "+" & myInt & "M"
            Log(msg)
        Case 10,11,12
            myInt = myInt / Power(10, 9)
            msg = "+" & myInt & "T"
            Log(msg)
    End Select
 
Last edited:
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
B4X:
Public Sub ShortenNumber(Value As Int) As String
    'https://en.wikipedia.org/wiki/Unit_prefix
    Dim unitprefix As String
    If Value >= 1000000 Then
        Value = Value / 1000000
        unitprefix ="M"
    Else If Value >= 1000 Then
        Value = Value / 1000
        unitprefix = "k"
    Else
        Return Value
    End If
    Return NumberFormat(Value ,1,0) & unitprefix
End Sub
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
Just for fun...

B4X:
    Dim origValue As Double = 2023123.345
    Dim logX As Int = Floor(Logarithm(origValue, 10))
    Dim result As String
    Select logX
        Case 0, 1, 2: result = Round(origValue)
        Case 3, 4, 5: result = Round(origValue / 1000) & "K"
        Case 6, 7, 8: result = Round(origValue / 1000000) & "M"
        Case 9, 10, 11: result = Round(origValue / 1000000000) & "B"
    End Select
    Log(result)        '2M
 
Upvote 0
Top