# B4J QuestionIssue with Double format

##### Member
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
Longtime User
Use NumberFormat or NumberFormat2 to display the values.

##### Member
Use NumberFormat or NumberFormat2 to display the values.
Thanks Steve I tried this but still get 26343.8 instead of 26343.80 using this method;

StoreTotal=Round(StoreTotal * 100) / 100
Log(NumberFormat(StoreTotal,0,2))

#### Erel

##### B4X founder
Staff member
Longtime User
Never use Round to convert a number to string.
B4X:
``````Dim d As Double = 26343.8
Log(NumberFormat2(d, 1, 2, 2, False))``````

#### stevel05

##### Expert
Longtime User
NumberFormat2 has a MinimumFractions parameter. Try:
B4X:
``Log(NumberFormat2(Storetotal,0,2,2,false))``
Log(NumberFormat2(Storetotal,0,2,2,false))

##### Member
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

#### Erel

##### B4X founder
Staff member
Longtime User
Thanks Steve both Log(NumberFormat2(Storetotal,0,2,2,false)) and Log(NumberFormat2(Storetotal,1,2,2,false)) show the total as 26343.79999
Can't be. It will only show 2 fraction digits.

Please post the relevant code and the logs.

##### Member
Can't be. It will only show 2 fraction digits.

Please post the relevant code and the logs.

Private StoreTotal as Double

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"))
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
51.8 KB · Views: 205

#### stevel05

##### Expert
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)``

##### Member
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)``
Thanks Steve that worked perfectly.

#### BeneBarros

##### Active Member
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

Replies
14
Views
1K
Replies
1
Views
2K
Replies
25
Views
23K
Replies
649
Views
87K
Replies
3
Views
2K