B4J Question [BANano]NumberFormat2 gives a different behavior in BANano than in B4J

Hi,
It says that NumberFormat and NumberFormat2 is supported in BANano but I get a different behavior than in B4J.
NumberFormat2 (Number As Double, MinimumIntegers As Int, MaximumFractions As Int, MinimumFractions As Int, GroupingUsed As Boolean) As String
Converts the specified number to a string.
The string will include -

  • at least Minimum Integers,
    at most Maximum Fractions digits and
    at least Minimum Fractions digits.
GroupingUsed - Determines whether to group every three integers.
If I use:
B4X:
NumberFormat2(314.159265359, 1, 2, 0, False))

In B4J I get what I want:
314.16

In BANano I only get (wrong)
314

If I change MinimumFractions to 2 then I get (this is ok but not right in the next example):
314,16

When I set the number to 0 (still a Double) I get what I do not want:
0,00

In B4J then I get what I want:
0
If I use original max 2 and min 0 which is the way I want to use the NumberFormat2.

I would say that this is a bug in BANano where the Maximum- and MinimumFractions does not work as intended but maybe there is a way to solve this ?
 
Solution
Will be fixed in the next update. (All but that weird 0 <> 0.00 one where I have no idea what the reasoning in Java could be. Until I do, this case will remain different).

Test cases:
B4X:
    Log(NumberFormat2(314.159265359, 1, 2, 0, False))
    Log(NumberFormat2(314.159265359, 1, 2, 2, False))
    Log(NumberFormat2(314.159265359, 1, 2, 4, False))
    Log(NumberFormat2(0, 1, 2, 0, False))
    Log(NumberFormat2(0, 1, 2, 2, False))
    Log(NumberFormat2(0, 1, 2, 4, False))

Results B4J:
B4X:
314.16
314.16
314.1593
0
0.00
0.0000

BANano Results:
B4X:
314.16
314.16
314.1593
0.00
0.00
0.0000

Here is a temporary workaround until the new release:
B4X:
public Sub NumberFormat2Fix(number As Double, minimumIntegers As Int...
Yes if I ask for minimum of two then I should get two (like this 0,00) but that is not my issue.
I only want minimum of 0 and maximum 2 but I do not get the maximum of 2 I only get 0 fractions regardless of the number of fractions I have in my number.
Just to be clear my issue is that I get 314 and not 314,16 with minimum fraction of 0 and maximum fraction of 2.
 
Last edited:
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
I would say that this is a bug in BANano where the Maximum- and MinimumFractions does not work as intended but maybe there is a way to solve this ?
Thanks a lot for discovering this. Whilst it is not always nice to be a "bug finder", the good thing is that it ends up with a positive ending, the bug is fixed and the program works as expected and thus is "stable".

Catching these..

In computer programming jargon, a heisenbug is a software bug that seems to disappear or alter its behavior when one attempts to study it.[1]

Should be difficult though.
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
Will be fixed in the next update. (All but that weird 0 <> 0.00 one where I have no idea what the reasoning in Java could be. Until I do, this case will remain different).

Test cases:
B4X:
    Log(NumberFormat2(314.159265359, 1, 2, 0, False))
    Log(NumberFormat2(314.159265359, 1, 2, 2, False))
    Log(NumberFormat2(314.159265359, 1, 2, 4, False))
    Log(NumberFormat2(0, 1, 2, 0, False))
    Log(NumberFormat2(0, 1, 2, 2, False))
    Log(NumberFormat2(0, 1, 2, 4, False))

Results B4J:
B4X:
314.16
314.16
314.1593
0
0.00
0.0000

BANano Results:
B4X:
314.16
314.16
314.1593
0.00
0.00
0.0000

Here is a temporary workaround until the new release:
B4X:
public Sub NumberFormat2Fix(number As Double, minimumIntegers As Int, maximumFractions As Int, minimumFractions As Int, groupingUsed As Boolean) As Double
    Return BANano.RunJavascriptMethod("NumberFormat2", Array(number, minimumIntegers, maximumFractions, minimumFractions, groupingUsed))
End Sub

#if JavaScript
function BANano_r2fFIX(number, decimals, minf) {
    var decimals2=minf;
    if (decimals2<decimals) {decimals2=decimals}
    if (decimals2>decimals) {decimals=decimals2}
    let v = +(Math.round(number + "e+" + decimals) + "e-" + decimals2);   
    var s = +v.toFixed(decimals2);
    if (s.countDecimals()<=minf) {
        return v.toFixed(minf);       
    } else {
        return v.toFixed(decimals2);       
    }   
};

Number.prototype.countDecimals = function () {
    if (Math.floor(this.valueOf()) === this.valueOf()) return 0;
    var str = this.toString();
    if (str.indexOf(".") !== -1 && str.indexOf("-") !== -1) {
        return str.split("-")[1] || 0;
    } else if (str.indexOf(".") !== -1) {
        return str.split(".")[1].length || 0;
    }
    return str.split("-")[1] || 0;
};

function NumberFormat2(number, minimumIntegers, maximumFractions, minimumFractions,groupingUsed) {
    return BANano_nf2(BANano_r2fFIX(number,maximumFractions,minimumFractions),minimumIntegers,groupingUsed);
}
#End If

Usage:
B4X:
    Log(NumberFormat2Fix(314.159265359, 1, 2, 0, False))
    Log(NumberFormat2Fix(314.159265359, 1, 2, 2, False))
    Log(NumberFormat2Fix(314.159265359, 1, 2, 4, False))
    Log(NumberFormat2Fix(0, 1, 2, 0, False))
    Log(NumberFormat2Fix(0, 1, 2, 2, False))
    Log(NumberFormat2Fix(0, 1, 2, 4, False))

Alwaysbusy
 
Last edited:
Upvote 1
Solution
Top