Assign Json String to Double

rildomoraes

Member
Licensed User
Longtime User
I consume a web service with JSON result thats works fine, but a value as string 4.10 is changed to 4 with this function:

B4X:
Sub ifNullDouble(cString As String, cDefault As Double) As Double
Dim l As Double

Try
   l = cString 
   Return l
Catch
   Return cDefault
End Try

End Sub

If I try to direct assign to Double var result in number exception format 4.10

Can anyone help me? :BangHead:

Regards,
 

rildomoraes

Member
Licensed User
Longtime User
B4X:
[
{
"Codigo":"02.12.02",
"Descricao":"LOMBINHO MAFRINORTE",
"Unidade":"KG",
"Embalagem":"CX",
"Estoque":"0",
"Grupo":"2",
"PrMinimo":"0.00",
"PrecoMedio":"3.60",
"PrecoVenda":"4.10",
"PrecoVenda2":"0.00",
"PrevisaoChegada":null,
"QtdUnd":"27.285714285710001",
"UltimaEntrada":null
},
{
"Codigo":"01.12.08",
"Descricao":"CORACAO DE FGO SEARA",
"Unidade":"KG",
"Embalagem":"CX",
"Estoque":"0",
"Grupo":"1",
"PrMinimo":"0.00",
"PrecoMedio":"7.21",
"PrecoVenda":"4.10",
"PrecoVenda2":"0.00",
"PrevisaoChegada":null,
"QtdUnd":"18",
"UltimaEntrada":null
}]

Column name is PrecoVenda

Regards,
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
Is it possible that you are assigning PrecoVenda to something that is rounding it down before passing it as cDefault? Have you tried adding a toast message to your sub that displays the value of cDefault when the sub is called? That should answer the question posed above.
 
Upvote 0

rildomoraes

Member
Licensed User
Longtime User
B4X:
LProdutos = parser.NextArray
         lstLog.AddSingleLine("Salvando Produtos...")
            For i = 0 To LProdutos.Size - 1
                Dim m As Map
            Dim sCodigo As String
            m.Initialize
                m = LProdutos.Get(i)
            DoEvents
            lbltotal.Text= (i+1) & "/"& (LProdutos.Size)
            pbSincronismo.Progress =( i * 100 / LProdutos.Size -1)
            sCodigo=(m.Get("Codigo"))
                pr.ID = sCodigo.replace(".","")
            
            pr.CodAux = (m.Get("Codigo"))
            pr.Descricao =Functions.ifNullString( m.Get("Descricao"),"")
            pr.Unidade = Functions.ifNullString(m.Get("Unidade"),"")
            pr.Embalagem = Functions.ifNullString(m.Get("Embalagem"),"")
            pr.Estoque = Functions.ifNullDouble(m.Get("Estoque"),0)
            pr.Grupo = Functions.ifNullDouble(m.Get("Grupo"),0)
            
            pr.PrecoMinimo = Functions.ifNullDouble(m.Get("PrMinimo"),0)
            pr.PrecoMedio =Functions.ifNullDouble( m.Get("PrecoMedio"),0)
                                'pr.PrecoVenda is Double
            pr.PrecoVenda =Functions.ifNullDouble( m.Get("PrecoVenda"),0)
                                Log("PrecoVenda " & pr.PrecoVenda) '<<<<<<<<<<<<<<<<<< Result is 4
 
Last edited:
Upvote 0

rildomoraes

Member
Licensed User
Longtime User
B4X:
m.Get("PrecoVenda")
results a Object. Can any problem or a bug to assign to Long or Double type?
:BangHead:
 
Upvote 0

rildomoraes

Member
Licensed User
Longtime User
I made a mistake. PrecoVenda in type pr was the only variable that was as Long. Thanks for all :sign0013:
 
Upvote 0
Top