3

We are trying to convert some qbasic scripts into python scripts. The scripts are used to generate some reports. Generally the reports generated by qbasic and python scripts should be exactly same. While generating a report we need to format a floating point number in a particular format. We use the following commands for formatting the number.

For QBASIC, we use

PRINT USING "########.###"; VAL(MYNUM$)

For Python, we use

print('{:12.3f}'.format(mynum))

where MYNUM$ and mynum having the floating point value.

But in certain cases, the formatted value differs between python and qbasic.

The result become as follows,

enter image description here

Can anyone help me to sort out this problem and make the python formatting work like qbasic?

RJ Anoop
  • 763
  • 13
  • 26

1 Answers1

3

This seems to be an related to the datatype (maybe 32bit float in qbasic and 64bit in python) used and how rounding is implemented. For example when you use:

from ctypes import c_float
print(floor(c_float(mynum).value*1000+.5)/1000)

c_float converts the python float into C format.

it will give me the numbers exactly in python exactly as in qbasic.

RJ Anoop
  • 763
  • 13
  • 26
Lee
  • 1,427
  • 9
  • 17