B4J Question Issue with Double format

Discussion in 'B4J Questions' started by Adrian Trantino, Aug 3, 2018.

  1. 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.
  2. stevel05

    stevel05 Expert Licensed User

    Use NumberFormat or NumberFormat2 to display the values.
    Adrian Trantino and Erel like this.
  3. Thanks Steve I tried this but still get 26343.8 instead of 26343.80 using this method;

    StoreTotal=Round(StoreTotal * 100) / 100
  4. Erel

    Erel Administrator Staff Member Licensed User

    Never use Round to convert a number to string.
    Dim d As Double = 26343.8
    Log(NumberFormat2(d, 122False))
    Adrian Trantino likes this.
  5. stevel05

    stevel05 Expert Licensed User

    NumberFormat2 has a MinimumFractions parameter. Try:
    Adrian Trantino and Erel like this.
  6. 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
  7. Erel

    Erel Administrator Staff Member Licensed User

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

    Please post the relevant code and the logs.
  8. Private StoreTotal as Double

    Sub Download_Click

    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

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

    End Sub

    Attached Files:

  9. stevel05

    stevel05 Expert Licensed User

    You need to apply the Numberformat2 to the total, it doesn't alter the variable but returns a formatted string.
    Total.Text= "$" & NumberFormat2(StoreTotal, 022False)
    Adrian Trantino and Erel like this.
  10. Thanks Steve that worked perfectly. :)
  11. BeneBarros

    BeneBarros Active Member Licensed User

    In this case you can use this function as well.

    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 =
       return currencyFormatter.format(value);
    #end if
    Result = Cr$ 45,00 - This for Brazil
    Adrian Trantino likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice