[Tutor] combine string and integer
Israel Evans
israel@lith.com
Wed, 22 May 2002 14:13:58 -0700
I don't know if this is the best way to do it but it seemed to work for me..
def numbermadness(num):
if num < 0:
strnum = str(num) # making num into a string
neg = strnum[0] # getting the - sign
number = strnum[1:len(strnum)]# getting the rest of num
num = number + neg # switching stuff around
return num # returning num whether it's changed
or not.
~Israel~
-----Original Message-----
From: Terje Johan Abrahamsen [mailto:terjeja@hotmail.com]
Sent: 22 May 2002 1:59 PM
To: tutor@python.org
Subject: [Tutor] combine string and integer
I have two variables that I want to merge into one based on a condition:
def amount(self):
if amount > 0:
amount = amount
else:
amount = amount, '-'
(or amount = amount * (-1), '-')
If the variable amount is a negative number, for example -25, I want this
function to remove the - in front of 25, and put it behind it, 25-.
I assume amount[1:9] can remove the - in front? Or does the slice notation
only work on strings? Otherways I could do the following amount*(-1)?
However, the main problem is to get the - to go behind the amount. I have
tried amount + "-", amount & "-" and amount, "-" as well as '-'. However,
the two first gives me an errormessage. (unnsupported operand for float and
string) The last one gives me this result: (-25.0, '-') A list I
suppose? However, I want 25- or 25.0-. It doesn't really matter if it
becomes a string or an integer. I am going to paste it into a different
program....
Thanks in advance,
Terje
_________________________________________________________________
Chat with friends online, try MSN Messenger: http://messenger.msn.com
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor