Unexpected PRINT USING ".##" Rounding for .xx5

This article was previously published under Q28164
This article has been archived. It is offered "as is" and will no longer be updated.
This article describes a case where the PRINT USING statement appearsto round numeric constants to unexpected values.

For the following statement
   PRINT USING " ###.##"; .245, .255, .265, .275, .285				
the above Basic versions give the following output
   0.25  0.25  0.26  0.28  0.28				
while you might have expected the following rounding
   0.25  0.26  0.27  0.28  0.29				
or with IEEE rounding (to the nearest even integral value), you wouldexpect the following:
   0.24  0.26  0.26  0.28  0.28				
This behavior occurs because the internal representation of thenumbers used by Basic (IEEE floating-point format) differs slightlyfrom the decimal numbers typed into the source code.

IEEE floating-point format cannot accurately represent numbers thatare not of the form 1.x to the power of y (where x and y are base 2numbers). The internal representation will be slightly more orslightly less than the decimal numbers typed into the source code.

The internal representations are correctly rounded and displayed inthe above program. This is not a software problem.

If you want floating-point constants in the source code to be exactlystored, you can append the constant with the CURRENCY type-castoperator, an "at sign" (@) character. The CURRENCY data type is onlyfound in Microsoft Basic PDS 7.00 and 7.10. The following examplerounds to 0.25, 0.26, 0.27, 0.28, 0.29 as you may have wanted:
   PRINT USING " ###.##"; .245@, .255@, .265@, .275@, .285@				
Note that the CURRENCY data type can store 19 digits, but only 4digits can be used after the decimal point.
QuickBas BasicCom

Article ID: 28164 - Last Review: 01/06/2015 00:38:17 - Revision: 2.1

  • Microsoft QuickBasic 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBasic 4.5 for MS-DOS
  • Microsoft BASIC Compiler 6.0
  • Microsoft BASIC Compiler 6.0b
  • Microsoft BASIC Professional Development System 7.0
  • Microsoft BASIC Professional Development System 7.1
  • kbnosurvey kbarchive KB28164