Italian [Risolto][b4j] calcolare numeri double con decimali

ivanomonti

Expert
Licensed User
Longtime User
devo calcolare decimali e double, ma non mi trovo nel gestire i numeri

esempio:

B4X:
Dim d As Double= 0.000002
d = (d*(Data.Length/4.5))/1000


Tabella:

$ x 1000 Token​
token​
$ x token​
token usati​
$ spesa token usati​
$ 0,002​
1000​
0,000002​
37​
0,000074​

Risultato con il codice (7.46666666666666)

1678755780470.png
 

ivanomonti

Expert
Licensed User
Longtime User
Cos'è Data.Length? Una stringa? 😮

Se non spieghi meglio le domande...!

calcolo token e costi query:
Private Sub TextField1_TextChanged (Old As String, New As String)
    
    Dim value() As String = Regex.Split(" ",New)
    Dim Data() As Byte
    Data = New.GetBytes("UTF8")
    
    Dim p As Pane = Sender.As(TextField).Parent
    For Each n As Node In p.GetAllViewsRecursive
        If n Is Label Then
            If n.Tag="id001-18" Then
                n.As(Label).Text=value.Length-1
            End If
            If n.Tag="id001-19" Then
                n.As(Label).Text=(Data.Length/4.5).As(Int)
            End If
            If n.Tag="id001-20" Then
                Dim d As Double= (0.000002*(Data.Length/4.5))/1000
                n.As(Label).Text= d
            End If
        End If
    Next
    
End Sub
 

LucaMs

Expert
Licensed User
Longtime User
Dim Data() As Byte

Data = New.GetBytes("UTF8")
e che senso ha dividere il numero di caratteri del testo immesso (new) per 4,5 ?
Per me è arabo, abbandono.

Posso solo suggertiti di usare tutte variabili Double e formattare l'output (visivo) con NumberFormat2, eventualmente invertendo punti e virgole, se necessario.
 
Last edited:

ivanomonti

Expert
Licensed User
Longtime User
e che senso ha dividere il numero di caratteri del testo immesso per 4,5 ?
Per me è arabo, abbandono.

Posso solo suggertiti di usare tutte variabili Double e formattare l'output (visivo) con NumberFormat2, eventualmente invertendo punti e virgole, se necessario.
sto calcolando i token e il loro costo

$0.002 è il costo di 1000 token
4,5 caratteri sono un token
la frase è di 168 caratteri e 27 parole (The decoder will subsample the bitmap if MaxWidth or MaxHeight are smaller than the bitmap dimensions. This can save a lot of memory when loading large images. Example:)
168/4.5 = 37 token
quindi il calcolo è

((0,002/1000)*37) = $0,000074 = costo dei token per la richiesta che fai a openai
o
0,0000002*37 = $0,000074 = costo dei token per la richiesta che fai a openai

in excel è una cagata qui divento scemo, non mi dire di usare excel :)

excel = =(0,002/1000)*54 result 0,000108 = perfect ( ma perchè così tortuoso b4j )


1678757986276.png
 
Last edited:

ivanomonti

Expert
Licensed User
Longtime User
ho risolto con questo calcolo.

In questo codice, abbiamo dichiarato tre variabili: "costoToken" per memorizzare il costo totale dei token utilizzati, "totaleToken" per memorizzare il numero di token utilizzati (che in questo esempio è 50) e "costoPerMilleToken" per memorizzare il costo per 1000 token (che in questo esempio è 0,002).

Il calcolo del costo totale dei token utilizzati è dato dalla moltiplicazione del numero di token utilizzati per il costo per mille token, diviso per 1000 (in modo da convertire il costo per mille token in costo per singolo token):


Calcolo:
Dim costoToken As Double
Dim totaleToken As Int = (Data.Length/4.5).As(Int)
Dim costoPerMilleToken As Double = 0.002
costoToken = totaleToken / 1000 * costoPerMilleToken
n.As(Label).Text= NumberFormat(costoToken, 1,10)
 
Top