B4J Question For computers we have KB, MB, GB, TB etc, for money?

emexes

Well-Known Member
Licensed User
Typed on phone so no guarantee, but try:
B4X:
Sub NumberSuffix(N As Double) As String

    If N < 0 Then
        Return "-" & NumberSuffix(-N)
    End If
 
    Dim Suffix() As String = Array As String("", "k", "M", "B", "T")
    '''Dim Suffix() As String = Array As String("", " Thousand", " Million", " Billion", " Trillion")

    Dim Thousands As Int = 0
    Do While N >= 1000 And Thousands < Suffix.Length - 1
        Thousands = Thousands + 1
        N = N / 1000
    Loop

    If Thousands = 0 Then
        '2 decimal places per example of monetary amount including cents
        Return NumberFormat2(N, 1, 2, 2, False)
    End If
 
    'sufficient decimal places for precision required
    Dim MaxDecimalPlaces As Int = 0
    Do While MaxDecimalPlaces < 5    'absolute maximum 5 decimal places
        Dim RelativeError As Double = Abs(N - Round2(N, MaxDecimalPlaces)) / N
        If RelativeError < 0.007 Then    'eg 0.7% (or whatever precision you need)
            Exit
        End If
        MaxDecimalPlaces = MaxDecimalPlaces + 1
    Loop
 
    Return NumberFormat2(N, 1, 0, MaxDecimalPlaces, False) & Suffix(Thousands)

End Sub
 
Last edited:

emexes

Well-Known Member
Licensed User
I need something to convert money figures to their equivalents just like one would convert sizes to MB etc.
Is there a general rule across the board for doing this. For example, $53,447,890.00, would be $53M.
Looks like you might also be headed towards something like this:
B4X:
For Each N In Array As Int(53447890, 53447090, 53440090, 53400090, 53000090)
    Log(N & " is: " & NumberToWords(N))
Next
B4X:
53447890 is: Fifty-Three Million, Four Hundred and Forty-Seven Thousand, Eight Hundred and Ninety
53447090 is: Fifty-Three Million, Four Hundred and Forty-Seven Thousand and Ninety
53440090 is: Fifty-Three Million, Four Hundred and Forty Thousand and Ninety
53400090 is: Fifty-Three Million, Four Hundred Thousand and Ninety
53000090 is: Fifty-Three Million and Ninety
in which case check out this post (sample) and the one after it (code):

https://www.b4x.com/android/forum/threads/how-to-read-a-number-to-word.107471/#post-672115
 
Last edited:
Top