Formatting decimals

NJDude

Expert
Licensed User
Longtime User
Is there a function to fix the number of decimal in B4A? something like the "FORMAT$" function in BASIC or "toFixed" in JavaScript?

For example, I need to work with only 2 decimal places:

The user inputs 4.34235334

and I need

Num = 4.34
 

kickaha

Well-Known Member
Licensed User
Longtime User
There are 2 methods:

As String
NumberFormat (Number, MinimumIntegers, MaximumFractions)
NumberFormat2 (Number, MinimumIntegers, MaximumFractions, MinimumFractions, GroupingUsed)

In your case:

InputNumber = 4.34235334
result = NumberFormat(InputNumber, 1, 2)

Result will be a string "4.34"

As number

Round2 (Number, DecimalPlaces)

In your case:

InputNumber = 4.34235334Result =
Round2 (InputNumber , 2)

Result will be a number 4.34
 
Last edited:
Upvote 0

CharlesR

Member
Licensed User
Longtime User
Is there a way to force the decimals to be displayed even if they are 0? For example 29.1 rounded to 2 decimal places will show 29.1 rather than 29.10. I know this may seem a strange request, but in some calculations it is better to show the zeros to confirm that the calculation has been to the appropriate level and for consistancy.

Thanks

Charles
 
Upvote 0

CharlesR

Member
Licensed User
Longtime User
Thank you for your prompt reply.

I have tried that without success. For example the following line of code returns 29 when I want to display 29.00. I would be grateful if you can tell me what I am doing wrong.

avgtemp = (NumberFormat2(29.0, 0, 2, 2, False))

Thank you in advance.

Charles
 
Upvote 0

CharlesR

Member
Licensed User
Longtime User
Thanks for that Erel. I guess that msgbox must have some embedded function to not display trailing zeros as I was outputing via msgbox.

Regards

Charles
 
Upvote 0

CharlesR

Member
Licensed User
Longtime User
Avgtemp is a double. Here is the code:

'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.

End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim bmstemp, thermotemp,guntemp, avgtemp As Double
Dim bmp As Bitmap
End Sub

Sub Activity_Create(FirstTime As Boolean)
Bmp.Initialize(File.DirAssets, "android48.png")
Log(NumberFormat2(29.0, 0, 2, 2, False))
getbmstemp
getthermotemp
getguntemp
showtemps
End Sub

Sub getbmstemp
Dim nd As NumberDialog
nd.Digits = 3
nd.Number = 290
nd.Decimal = 1
nd.ShowSign = False
ret = nd.Show("Enter BMS Temperature reading", "Done", "", "",Bmp)
bmstemp = nd.Number/10
End Sub

Sub getthermotemp
Dim nd As NumberDialog
nd.Digits = 3
nd.Number = 290
nd.Decimal = 1
nd.ShowSign = False
ret = nd.Show("Enter Thermometer Temperature reading", "Done", "", "",Bmp)
thermotemp = nd.Number/10
End Sub

Sub getguntemp
Dim nd As NumberDialog
nd.Digits = 3
nd.Number = 290
nd.Decimal = 1
nd.ShowSign = False
ret = nd.Show("Enter Gun Thermometer Temperature reading", "Done", "", "",Bmp)
guntemp = nd.Number/10
End Sub

Sub Showtemps
avgtemp = (NumberFormat2((bmstemp + thermotemp + guntemp)/3, 0, 1, 1, False))
Log(NumberFormat2(29.0, 0, 1, 1, False))
Msgbox("BMS " &bmstemp & CRLF & "Thermometer " & thermotemp & CRLF & "Gun " & guntemp & CRLF & "Average " & avgtemp, "Today's readings are")
End Sub

Sub Showdate
DateTime.DateFormat = "dd-MM-yyyy"
Dim now As Long
now = DateTime.Now
Dim s As String
s = DateTime.Date(now)
Msgbox(s,"")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Numberformat and Numberformat2 return strings. What is happening in your program is you are re-converting them to numbers.
 
Upvote 0

CharlesR

Member
Licensed User
Longtime User
Thank you all for your help. It was not the lack of documentation which caused the problem, but rather my failure to realise that in order to show a fixed number of decimals, I needed to convert a number to a string.

Regards

Charles
 
Upvote 0
Top