B4J Question Issue with Double format

Hi Guys,

When using a float to display a sales total I am using this to round the amount.

StoreTotal=Round(StoreTotal * 100) / 100

It seems to work fine until I get a 0 in the last decimal place so 99.99 works fine, however 99.90 will display as 99.9

When I was using a float also with this StoreTotal=Round(StoreTotal * 100) / 100 it would display two decimal places perfectly when I would run in breakpoints, however as soon as the breakpoints were removed it would display all the decimals.
 

stevel05

Expert
Licensed User
Longtime User
NumberFormat2 has a MinimumFractions parameter. Try:
B4X:
Log(NumberFormat2(Storetotal,0,2,2,false))
Log(NumberFormat2(Storetotal,0,2,2,false))
 
Upvote 0
NumberFormat2 has a MinimumFractions parameter. Try:
B4X:
Log(NumberFormat2(Storetotal,0,2,2,false))
Log(NumberFormat2(Storetotal,0,2,2,false))
Thanks Steve both Log(NumberFormat2(Storetotal,0,2,2,false)) and Log(NumberFormat2(Storetotal,1,2,2,false)) show the total as 26343.79999..... In MySql table the sales were stored as decimal but I have changed them to Double and Float and still no change
 
Upvote 0
Can't be. It will only show 2 fraction digits.

Please post the relevant code and the logs.

Private StoreTotal as Double

Sub Download_Click
TableView1.Items.Clear


Dim RS As ResultSet = sql1.ExecQuery("Select d.EndDate, n.DeptName, d.SellInc, d.GP from DailyDeptSales d, Dept n, Store s where d.DeptCode = n.DeptCode and d.StoreCode = s.Code and StoreCode = ' " & StoreCode & " ' and EndDate=' " & SalesDate & " ' ")

Do While RS.NextRow

Log(RS.GetString("DeptName") & ", " & RS.GetString("SellInc") & ", " & RS.GetString("GP"))
Dim Row() As Object = Array (RS.GetString("DeptName"), "$" & RS.GetString("SellInc"), RS.GetString("GP"))
TableView1.Items.Add(Row)
StoreTotal=RS.GetString("SellInc") + StoreTotal
Loop
RS.Close

Log(NumberFormat2(StoreTotal, 0, 2, 2, False))
Total.Text= "$" & StoreTotal
Total.Visible=True
StoreTotal = 0

End Sub
 

Attachments

  • Capture.PNG
    Capture.PNG
    51.8 KB · Views: 250
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You need to apply the Numberformat2 to the total, it doesn't alter the variable but returns a formatted string.
B4X:
Total.Text= "$" & NumberFormat2(StoreTotal, 0, 2, 2, False)
 
Upvote 0

BeneBarros

Active Member
Licensed User
Longtime User
Thanks Steve that worked perfectly. :)

In this case you can use this function as well.
B4X:
Log(inline.RunMethod("displayCurrency",Array(45.0)))

Private Sub inline As JavaObject
    Return Me
End Sub

#if java
import java.util.*;
import java.text.NumberFormat;

static public String displayCurrency(Double value) {
   Locale currentLocale = Locale.getDefault();
   Currency currentCurrency = Currency.getInstance(currentLocale); 
   NumberFormat currencyFormatter =
   NumberFormat.getCurrencyInstance(currentLocale);
   return currencyFormatter.format(value);
}
#end if
Result = Cr$ 45,00 - This for Brazil
 
Upvote 0
Top