[Tutor] combine string and integer

Sean 'Shaleh' Perry shalehperry@attbi.com
Wed, 22 May 2002 14:12:10 -0700 (PDT)


On 22-May-2002 Terje Johan Abrahamsen wrote:
> 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....
> 

if value < 0:
  char = '-'
else:
  char = ''

return "%d%s" % (abs(value), char)