Bug? NumberFormat & NumberFormat2 not formating decimals

Discussion in 'B4J Bugs & Wishlist' started by ggpanta, Mar 28, 2014.

  1. ggpanta

    ggpanta Member Licensed User

    Dim fiat As double
    fiat = 6368.4397

    NumberFormat(fiat, 0, 2)

    Expected:
    textFiat = 6368.43

    Actual:
    textFiat = 6368.4397

    Edit: changed fiat to textFiat in expected & actual to make it clearer that its not expected to format the original var but the output of numberforma to a text obj
     
    Last edited: Mar 29, 2014
  2. LucaMs

    LucaMs Expert Licensed User

    Code:
    Dim fiat As Doubl
    fiat = 
    6368.4397

    Log(NumberFormat(fiat, 02))
    I get:
    6,368.44 (italian punctuation)

    rounded.
     
  3. klaus

    klaus Expert Licensed User

    @ggpanta
    NumberFormat(fiat, 0, 2) does not round the number it returns a string that is used for display.
    If you want to round the number you'd use Round2(Number As Double, DecimalPlaces As Int).
     
  4. ggpanta

    ggpanta Member Licensed User

    @LucaMs unfortunatelly I cant make it work for some reason

    @klaus thats what I am rounding in my code a string and for some reason it doesnt work... textfield1= NumberFormat(fiat, 0, 2) should have only 2 decimals but I am not seeing that :(

    I will have to look at the code maybe I am doing something wrong that I am not seeing.
     
  5. LucaMs

    LucaMs Expert Licensed User

    I'm sure that your code is:
    textfield1.text = ...

    fiat is a String variable?

    if so, you can try something like:

    Code:
    dim dblFiat as Double = fiat
    dblFiat = 
    Round2(dblFiat, 2)
    textfield1.text = dblFiat
     
  6. ggpanta

    ggpanta Member Licensed User

    I dont want rounding to happen at all though, I need the extra decimals for calculations but I only want to show 2 decimals in the string. This is a cryptocurrency app and decimals are very important, even 0.0000001 of a coin has actual monetary value, so the decimals must stay as they are, what I show to the user in some parts (summary) just doesnt need them, I am cutting the string now so its ok but for some reason numberformat doesnt want to play.
     
  7. LucaMs

    LucaMs Expert Licensed User

    Try to create a small project for test that contains only the variable and the EditText.

    If it does not work, post it here.

    (I do not know if this affects, but you can also check the InputType of the EditText)
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    NumberFormat(fiat, 02)
    The above line doesn't do anything.

    Try this:
    Code:
    Dim formattedNumber As String = NumberFormat(fiat, 02)
    Log(formattedNumber)
    Log(fiat)
     
  9. ggpanta

    ggpanta Member Licensed User

    This is the exact code in the app:

    Code:
    JSON.Initialize(APIReply)
        
    Dim mapAPI As Map
        mapAPI = JSON.NextObject
        
    Dim fiat As Double
        fiat = mapAPI.Get(
    "last")
        lblExchangeValue.Text = 
    NumberFormat(fiat, 02)
    lblExchangeValue is cast as label
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    I've just made a small test and NumberFormat returns the expected results. If you like you can upload a small project that demonstrates this issue.
     
  11. ggpanta

    ggpanta Member Licensed User

    Its obviously something I am doing, I will retrace the whole thing to check where I am messing up, you can close the post if you want :)
     
  12. ttsolution

    ttsolution Member Licensed User

    Same to me, It works most of time but some time it do not works. If I restart the App it then works

    Jonh,
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    I can't see any reason for such behavior. If you can reproduce it then please upload a simple project for this.
     
  14. Rusty

    Rusty Well-Known Member Licensed User

    Here is a small example of what is being said:
    Code:
    Sub Process_Globals
    End Sub

    Sub Globals
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        
    Dim fiat As Double
        fiat = 
    6368.4397
    Log("With decimal values not=zero: " & NumberFormat(fiat, 02))
     
        fiat = 
    6368.0            '4397
    Log("With decimal value of zero: " & NumberFormat(fiat, 02))
     
    End Sub

    Sub Activity_Resume
    End Sub

    Sub Activity_Pause (UserClosed As Boolean)
    End Sub
    RESULTS:
    I have exactly the same problem any double with a .0 comes out as an integer without the .0 decimal. i.e. 91.0 = 91 ; 91.1 = 91.1 ...

    Any help is apprecated.
     
  15. klaus

    klaus Expert Licensed User

    What exactly do you expect ?
    NumberFormat(Number As Double, MinimumIntegers As Int, MaximumFractions As Int)
    returns 2 fractions max but removes any non significant zeros.
    If you want at least one fraction you should use NumberFormat2:
    NumberFormat2(Number As Double, MinimumIntegers As Int, MaximumFractions As Int, MinimumFractions As Int, GroupingUsed As Boolean)
     
    Erel and LucaMs like this.
  16. Rusty

    Rusty Well-Known Member Licensed User

    Aha! Thanks Klaus!
    I was unaware of numberformat2, don't know how I missed this.
    I was expecting the zero to remain...my bad :)
    Rusty
     
Loading...
  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