Reputation: 57
I'm having a hard time converting a passed in argv[1] to a float number. I have been trying to basically pass in any int followed by a decimal and have it round up/down to the nearest whole number. (basic starter python project) I have been trying to play with float for a bit now and just cant figure it out.
import sys
x = int(sys.argv[1])
num = x + .5
s = str(num())
point = s.find('.')
print s[:point]
if __name__ == "__main__":
x(int(sys.argv[1]))
Edit(current code, after coffee and cleanup):
import sys
def x(argument):
num = argument + .5
s = str(num())
point = s.find('.')
print s[:point]
if __name__ == "__main__":
x(int(round(float(sys.argv[1]))))
Finalized code(credit goes to: NPE)
import sys
def x(argument):
print "Passed in value: " + str(argument)
num = argument + .5
print "Rounded whole number: " + str(int(num))
if __name__ == "__main__":
x(float(sys.argv[1]))
Upvotes: 2
Views: 10853
Reputation: 500257
It sounds like you're looking for either
val = round(float(sys.argv[1]))
or
val = int(round(float(sys.argv[1])))
depending on what you're expecting the type of val
to be.
Upvotes: 4