Android Question Howto convert Byte in Mbyte

FabioG

Active Member
Licensed User
Longtime User
Hello,

I need help to convert from bytes to megabytes

use this code:
B4X:
    Dim TxData_Tmp As Long
    Dim RxData_Tmp As Long
    Dim TxData As Long
    Dim RxData As Long

    TxData_Tmp = File.ReadString(File.DirInternal,"tx_bytes")
    RxData_Tmp = File.ReadString(File.DirInternal,"rx_bytes")
       
    TxData = (TxData_Tmp/1048576*100000)/100000 'Mbyte
    RxData = (RxData_Tmp/1048576*100000)/100000 'Mbye   
   
    Label_TxData.Text = "TX Data: " & RxData & " MB"
    Label_RxData.Text = "RX Data: " & TxData & " MB"

but I will always return a number in the entire
I need to have a non-integer number such

0.8 Mb or 1.2 Mb

who can help me?

thank you very much
 

blong

Active Member
Licensed User
Longtime User
You are using integer values for the values ... use floating point values.

And then a format statement to get single decimal place.
 
Upvote 0

FabioG

Active Member
Licensed User
Longtime User
ok solved

Thanks :)

B4X:
Dim TxData_Tmp As Long
    Dim RxData_Tmp As Long
    Dim TxData As Float
    Dim RxData As Float

    TxData_Tmp = File.ReadString(File.DirInternal,"tx_bytes")
    RxData_Tmp = File.ReadString(File.DirInternal,"rx_bytes")
      
    TxData = (TxData_Tmp/1048576*100000)/100000 'Mbyte
    RxData = (RxData_Tmp/1048576*100000)/100000 'Mbye 
  
    Label_TxData.Text = "TX Data: " & Round2(RxData,2) & " MB"
    Label_RxData.Text = "RX Data: " & Round2(TxData,2) & " MB"
 
Upvote 0
Top