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.
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).
Although the behaviour is indeed different, I honestly find what B4J (Java) produces confusing. If I ask for a minimum of two fractions, I would expect 0,00 not 0
I see quite some posts with questions on how numberformat works and what people expect as result.
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.
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]
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).
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