Bug? Round2 with high precision parameters

ShepSoft

Member
Licensed User
round2.jpg

This is not really a significant difficulty and I am adding this note here as a point of awareness because I have seen quite a few comments about rounding numbers in these forums.

Using Round2() with a high precision as the second parameter fails as shown here. The maximum precision of a (B4X/Java) Double is just under 16 digits. That precision can rise if the majority of the digits are in the mantissa (as in the given example).

It would be better if the parser reduced the precision requested to a maximum of 17.

Failing that, be aware that you may get (seriously) erroneous answers if you use high precisions with Round2().
 

toby

Well-Known Member
Licensed User
Longtime User
I also found that larger the number, quicklier it fails.
B4X:
Log(Round2(12345678901234567890.123456789, 1)) '1 digit precision

result from above line of code: 9.2233720368547763E17 which is quite different from what's expected: 12345678901234567890.1
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
I recommend developers to never use Round2. It was a mistake to implement it as there are (almost) no good use cases for Round2. If you want to show a rounded number (which is a common use case) then use NumberFormat or B4XFormatter.

If you need 19 digits accuracy then you should use BigNumbers library.
 

ShepSoft

Member
Licensed User
I recommend developers to never use Round2. It was a mistake to implement it as there are (almost) no good use cases for Round2. If you want to show a rounded number (which is a common use case) then use NumberFormat or B4XFormatter.

If you need 19 digits accuracy then you should use BigNumbers library.

Yes absolutely but the point of the post was just to illustrate the issue so users could avoid it.
 
Top