Android Question format integer into decimal value?

techknight

Well-Known Member
Licensed User
Longtime User
I think its numberformat, or numberformat2, but I cannot recall how to set it up its been awhile.

I am trying to take an integer value, such as 123. I want this value 1.23 or if i do 745 its 7.45.

I am taking an integer, and displaying it as a decimal in a label. so it would be 123 = "1.23"

any ideas?
 

DonManfred

Expert
Licensed User
Longtime User
Try this...

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Dim number As Int
    number = 123
    IntToFloat(number,10)
    IntToFloat(number,100)
    IntToFloat(number,1000)
End Sub
Sub IntToFloat(src As Int, devider As Int) As String
    Log(">IntToFloat("&src&","&devider&"):")
    Dim intasfloat As Float
    intasfloat = src
    Log (">"&NumberFormat(intasfloat,0,2))
    intasfloat = intasfloat / devider
    Log ("<"&NumberFormat(intasfloat,0,2))
    Return NumberFormat(intasfloat,0,2)
End Sub


IntToFloat(123,10):
> 123
< 12.3

IntToFloat(123,100):
> 123
< 1.23

IntToFloat(123,1000):
> 123
< .12
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Didnt work. At least not correctly.

If the value is 0, I get 0 instead of 0.00
IntToFloat(NumberFormat(Regular, 3,2), 100)

If I try it this way,
lblRegular.Text = "$" & NumberFormat(IntToFloat(Regular, 100), 3,2)

I get 000 instead of 0 or 0.00

This has to be setup so if the value is say, 5, it is 0.05. or if its 50, its 0.50, or if its 500, its 5.00

I am making a pricing app.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Nevermind, I figured it out.
lblRegular.Text = "$" & NumberFormat2(IntToFloat(Regular, 100), 1,2,2,False)
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You don't need to cast the value to a float, as NumberFormat2 requires a Double, it will automatically cast the value to a Double anyway. You can just do:

lblRegular.Text = "$"&NumberFormat2(Regular / 100,1,2,2,False)
 
  • Like
Reactions: eps
Upvote 0
Top