Important Notice
The pages on this site contain documentation for very old MS-DOS software,
purely for historical purposes.
If you're looking for up-to-date documentation, particularly for programming,
you should not rely on the information found here, as it will be woefully
out of date.
Article Q35826, Method 3
◄Contents► ◄Index► ◄Back►
─────────────────────────────────────────────────────────────────────────────
◄Knowledge Base Contents► ◄Knowledge Base Index►
IEEE vs. Microsoft Binary Format; Rounding Issues (Complete) - Q35826
Method 3
--------
This method requires the use of a Microsoft C version 5.x or
later compiler. It uses the C library routine sprintf(). This
routine takes formatted screen output and stores it in a string
variable.
C Routine:
struct basic_string {
int length;
char *address;
} ;
void round(number,string)
double *number;
struct basic_string *string;
{
sprintf(string->address,"%.2f",*number);
}
Basic Program:
' To try this example in VBDOS.EXE:
' 1. From the File menu, choose New Project.
' 2. Copy the code example to the Code window.
' 3. Press F5 to run the program.
DECLARE SUB Round CDECL (number#, answer$)
CLS
b# = .05#
FOR i = 1 TO 10
b# = b# + .01#
answer$ = SPACE$(50)
CALL Round(b#, answer$)
PRINT b#, LTRIM$(RTRIM$(answer$))
PRINT
cnt = cnt + 4
IF cnt > 40 THEN
cnt = 0
INPUT a$
END IF
NEXT i
The same screen formatting can be accomplished with Basic's PRINT
USING statement. However, Basic has no direct means of storing this
information in a string. The information can be sent to a
Sequential file and then read back into string variables.
You can also write the information to the screen and read this
information using the SCREEN function. The SCREEN function returns
the ASCII value of the specified screen location. Consider the
following example:
' To try this example in VBDOS.EXE:
' 1. From the File menu, choose New Project.
' 2. Copy the code example to the Code window.
' 3. Press F5 to run the program.
x# = 7.000000000000001D-02
CLS
LOCATE 1, 1
PRINT USING "#################.##"; x#
FOR i = 1 TO 20
num = SCREEN(1, i)
SELECT CASE num
CASE ASC(".")
number$ = number$ + "."
CASE ASC("-")
number$ = "-"
CASE ASC("0") TO ASC("9")
number$ = number$ + CHR$(num)
CASE ELSE
END SELECT
NEXT i
PRINT number$
The PRINT USING statement would display 17 spaces and then .07. The
value of number$ would be .07.