The STR(x,l,p) and CAST(x to N(l,p))
should convert a number
to the string representation rounded with decimal p of the number x.
This doesn't happen and when the value is at least .n..n4995 VFP it rounds up.
Repro:
CLEAR
SET POINT TO
z=0.5
y=0.4994
? y,STR(y,10,0),CAST(y as N(10,0)),ROUND(y,0) at 50
x=0.4995
? x,STR(x,10,0),CAST(x as N(10,0)),ROUND(x,0) at 50
x=0.264995
? x,STR(x,10,2),CAST(x as N(10,2)),ROUND(x,2) at 50
x=0.264996
? x,STR(x,10,2),CAST(x as N(10,2)),ROUND(x,2) at 50
SET TEXTMERGE TO "errors.txt" ON NOSHOW
FOR n=0 TO 4
WAIT WINDOWS "TEST"+STR(N) NOWAIT
FOR j=MAX(10^(n-1),1) TO MAX(10^n-1,1)
dd1='0.'+LEFT(TRANSFORM(j),N)+'4'
FOR k=990 TO 999
x=VAL(m.dd1+TRANSFORM(K))
IF STR(x,10,n)==STR(ROUND(x,n),10,n)
\ <<ROUND(X,N+4)>>
LOOP
ENDIF
\ <<ROUND(X,N+4)>> <<STR(x,10,n)>> <<CAST(X AS N(10,n))>> <<ROUND(X,N)>>
NEXT
\
NEXT
NEXT
SET TEXTMERGE TO
MODIFY FILE "errors.txt"
The thing becomes a tragic comedy when is read
http://support.microsoft.com/kb/157954/en-usOn this it is said that ROUND() it is not exact,
and as workaround is used the VAL() of a STR() conversion.
BUG: ROUND() Function Not Rounding Properly After Calculations
RESOLUTION
....
The following code example provides a workaround by converting the numeric contents of mpay to a string.
Then the VAL() function converts the string back to a numeric data type for use in the ROUND() function.
...
But STR() have a round bug!
Then VFP is in short-circuit, and cannot round a number exactly.
Someone can put hand to ROUND(), STR() and CAST().
Thanks.
Fabio