x1# = 10051.49999999999999999999999999999 ' :(
PRINT "Rounded to 5 places: "; RoundDblX2DP$
(x1#
, 5) PRINT "Rounded to 2 places: "; RoundDblX2DP$
(x1#
, 2) PRINT "Rounded to 0 places: "; RoundDblX2DP$
(x1#
, 0) ?
PRINT "Rounded to Tens: "; RoundDblX2DP$
(x1#
, -1) 'just out of curiousity is this 0 in the 1's place ? yes! PRINT "Rounded to 100's: "; RoundDblX2DP$
(x1#
, -2)
' OK x <> .555... .55 OK
'for DP = number of decimal places
r = 5 * 10 ^ (-1 * (DP + 1))
'PRINT r 'that's it!
RoundDblX2DP$ = test$
The site reported by Fellippe probably tells us that the problem is without solution.
In some cases this is a big problem! For example if the discriminant is 1.49 (or 1.4999999) rather than 1.5 or 1.51.
See how the first three cases give an erroneous laughter.
In the end, a conversion into a string as it is would be enough, without automatic rounding (even sMcNeill has provided a starting point to operate in this sense, however impractical).
If the number could be turned into a literal string, then everything would be possible.
But maybe that's too much to ask, I don't know... I think only Fillippe could do something about it at the source code level.
but it's a shame ... I don't know the extent of this problem on some of my applications, but I think the impact is strong
You misunderstand how to use this function which only controls the display of numbers not the floating point math behind the scenes.
Only the first 14 to 16 digits are accurate in floating point math, the rest is always garbage.
So when you list more digits than a floating math memory address can hold of course it is going to chop off what it can't hold, it is generous that it does round at chop off point.
As applied to function, you asked for Rounded and for rounding to take place you need a cutoff of decimal places that is what DP parameter is for. Not any DP will work because of the 14-16 digit limit with Floating math.
If you test 15 digits or less the function rounds properly.
If you test more than 14-16 digits the floating point math will cut the number off at 14-16 digits (in base 10) because that is all the memory slot can hold.
Repeat: you only get 14-16 digits that will be treated in Floating point math.
As Steve said if you need higher precision than 14-16 digits then you have to write your own floating point math subs.
BTW I fixed the function to remove the extra decimal garbage creeping in, but again it will only work for 14-16 digits max, so the DP is limited to 14-16
minus the number of digits in the integer part of number.
'_title "RoundDblX2DP$ test sub" b+ 2020-08-09
' 2020-08-09 revise function name as _TITLE
' trim excess tail if any and don't add decimal and 0's if not any
x1# = 10051.555555555555555555555555555 'Great!
x1# = .3333333333333 'Great!
x1# = .888888888888 'Great
x1# = 5.55555555555 ' Fabulous!
x1# = .444444444444 ' Marvelous!
x1# = 73.737373737373 'Perfecto!
x1# = .666666666666666 'good again!
x1# = .54545454545 'Fine
x1# = .4999999999 ' 10 places OK kept all the 9's
x1# = .4555555 ' just lovely
x1# = .5 'fine too though expecting 0's
x1# = .55 'OK
x1# = .655 'OK
x1# = .6555 'OK
x1# = .65555 'ok
x1# = .655555 'ok
x1# = 1.4999999999999990 'copied from forum
x1# = .555 'here it starts! 5 places good 2 places and more decimals sneek in FIXED!
x1# = .5555 'craps again FIXED!
x1# = .5555555 'WTH where are the decimals coming from? FIXED
'try some lower and higher numbers
x1# = 100000.000005 'no round?
x1# = 100000.0000051 ' that should round
x1# = 100000.00500001
x1# = .005005005005
x1# = .0505050505
x1# = 10051.4999999999 ' 10 decimal place 15 digits total
'comment the above line and try others
PRINT "Rounded to 10 places: "; RoundDblX2DP$
(x1#
, 10) PRINT "Rounded to 5 places: "; RoundDblX2DP$
(x1#
, 5) PRINT "Rounded to 2 places: "; RoundDblX2DP$
(x1#
, 2) PRINT "Rounded to 0 places: "; RoundDblX2DP$
(x1#
, 0) PRINT "Rounded to Tens: "; RoundDblX2DP$
(x1#
, -1) 'just out of curiousity is this 0 in the 1's place ? yes! PRINT "Rounded to 100's: "; RoundDblX2DP$
(x1#
, -2)
' OK x <> .555... .55 OK
'for DP = number of decimal places
r = 5 * 10 ^ (-1 * (DP + 1))
'PRINT r 'that's it!
RoundDblX2DP$ = test$
BTW the function is for displaying numbers, it doesn't work miracles with floating point math.