Note that there are some explanatory texts on larger screens.

plurals
  1. POprecision of number held in a variable
    text
    copied!<p>I have the following code: </p> <pre><code>import os reader=open('try.txt', 'r') data=reader.read() reader.close() x=0.0 y=0.0 z=0.0 def xcoord(): global x global y global z while x&lt;=1.0: mypath1='C:\\Users\\Rebecca\\Documents\\PhD\\Learnpython\\X' + str(x) os.makedirs(mypath1) ycoord() x+=0.01 y=0.0 z=0.0 def ycoord(): global x global y global z while y&lt;=1.0: mypath2='C:\\Users\\Rebecca\\Documents\\PhD\\Learnpython\\X' + str(x) + '\\Y' + str(y) os.makedirs(mypath2) zcoord() y+=0.01 z=0.0 def zcoord(): global z while z&lt;=1.0: writer=open('C:\\Users\\Rebecca\\Documents\\PhD\\Learnpython\\X' + str(x) + '\\Y' + str(y) + '\\X' + str(x) + 'Y' + str(y) + 'Z' + str(z) + '.dat', 'a') writer.write(data) print('interstitial Ce' + ' %.2f' % x + ' %.2f' % y + ' %.2f' % z + ' fix', file=writer) print('centre' + ' %.2f' % x + ' %.2f' % y + ' %.2f' % z, file=writer) print('size 14 28', file=writer) print('dump1 inter' + '%.2f' % x + '%.2f' % y +'%.2f' % z + '.dat', file=writer) print('output xyz ' + '%.2f' % x + '%.2f' % y + '%.2f' % z + '.xyz', file=writer) writer.close() z+=0.01 xcoord() </code></pre> <p><strong>[N.B. I am aware using global variables in this way is not ideal but it this is currently the only way I can get my head round the code and get it to do what I want....]</strong></p> <p>When this is executed, the values of x, y and z increase as required, but (only sometimes) output value has several decimal places. </p> <p>How do I alter the setting such that x, y and z are only ever to 2 decimal places?</p> <p>Any help would be appreciated - cheers</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload