This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
rubyrubyrubyruby's profile picture

store numbers in file without a decimal

Started by rubyrubyrubyruby, 13 April 2013 - 12:49 AM
rubyrubyrubyruby #1
Posted 13 April 2013 - 02:49 AM
Hi ive got a problem with my program. this program is made for making a file with locations (it's not finished i experiment a lot in small programs)
the locations are numbers with a x (row) and an y (long) coordinate. but when it writes a line to the file he places a decimal to the line. this is the program:


term.clear()
term.setCursorPos(1,1)
abc = fs.exists("storevars")
  print("how many rows do youve got?")
  write("rows:")
  row_input = io.read()
  row_input = tonumber(row_input)
  print("how many chest do you got in a row?")
  write("this many:")
  long_input = io.read()
  long_input = tonumber(long_input)
  starterrow_input = 1
  starterlong_input = 1
  storevars = fs.open("storevars", "w")
  long_input = long_input+1
  row_input = row_input+1

   while starterrow_input < row_input do
	while starterlong_input < long_input do
  
	 writing = starterrow_input*100+starterlong_input
		storevars.writeLine(writing)
	 print("wrote "writing" to the file")
	 starterlong_input=starterlong_input+1
   end
   starterrow_input=starterrow_input+1
   starterlong_input=1
  end
  storevars.close()
  print("done")


and this is an examples of what it can make

101.0
102.0
103.0
104.0
105.0
106.0
107.0
108.0
109.0
110.0
201.0
202.0
203.0
204.0
205.0
206.0
207.0
208.0
209.0
210.0
301.0
302.0
303.0
304.0
305.0
306.0
307.0
308.0
309.0
310.0
401.0
402.0
403.0
404.0
405.0
406.0
407.0
408.0
409.0
410.0
501.0
502.0
503.0
504.0
505.0
506.0
507.0
508.0
509.0
510.0
how can you let the program write no decimals?
Frederikam #2
Posted 13 April 2013 - 03:27 AM
This is maybe not exactly the answer you are looking for, but why would you need this since you can use the tonumber() function?
LordIkol #3
Posted 13 April 2013 - 04:13 AM
Not sure if this what you search for but i think math.floor or math.ceil could do what you want
Have a look here for more info
http://lua-users.org/wiki/MathLibraryTutorial
LBPHacker #4
Posted 13 April 2013 - 04:23 AM
This is maybe not exactly the answer you are looking for, but why would you need this since you can use the tonumber() function?
+1 there. As far as I know, there's no official way to remove the decimal (of course you can write a function to do that). But anyways, if you just want to read it as a number, use tonumber(). That's the best way to do it.

EDIT: Figured that if you really want to write the numbers to the file without decimals, you can give .writeLine a string. Use tostring():
storevars.writeLine(tostring(writing))
rubyrubyrubyruby #5
Posted 16 April 2013 - 08:34 AM
thx the

storevars.writeLine(tostring(writing))
really did the trick!