need to add '%' symbol to my float type dataframe in python - Stack Overflow
Python 2.7: %d, %s, and float() - Stack Overflow
What does the float function do?
Adding $ or % in python without space in between the symbol and number.
You should look into pyformat when you only want to play around with the string representation.
>>> cash = 13.14
>>> '${}'.format(cash)
'$13.14'
>>> cash = 3.4
>>> '${:.2f}'.format(cash)
'$3.40'
>>> values = [1.2, 65.32, 15.2345]
>>> template = 'Stuff costs ${:.2f}.'
>>> for value in values:
print(template.format(value))
Stuff costs $1.20.
Stuff costs $65.32.
Stuff costs $15.23.You could also play around with object oriantation and manipulate the specific string manipulation:
>>> class Dollar:
def __init__(self, value):
self.value = value
def __add__(self, other):
return Dollar(self.value + other.value)
def __sub__(self, other):
return Dollar(self.value - other.value)
def __repr__(self):
return 'Dollar({})'.format(self.value)
def __str__(self):
return '${:.2f}'.format(self.value)
>>> d1 = Dollar(1.2)
>>> d2 = Dollar(4.63)
>>> d1
Dollar(1.2)
>>> str(d1)
'$1.20'
>>> d2
Dollar(4.63)
>>> str(d2)
'$4.63'
>>> d1+d2
Dollar(5.83)
>>> d1-d2
Dollar(-3.4299999999999997)
>>> print(d1+d2)
$5.83
>>> print(d1-d2)
$-3.43
>>> price = Dollar(0.99)
>>> 'The price is {}'.format(price)
'The price is $0.99'
See, with this double underscore methods you can mess around with the power of python. __add__ let's you override the plus operator, __str__ controls the string conversion and __repr__ the internal interpreter representation.
Of course you should convert your values to Decimal compute with that to avoid aweful rounding errors:
>>> from decimal import Decimal
>>> str(Decimal('0.1'))
'0.1'
>>> class Dollar:
def __init__(self, value):
self.value = Decimal(str(value))
def __add__(self, other):
return Dollar(self.value + other.value)
def __sub__(self, other):
return Dollar(self.value - other.value)
def __repr__(self):
return 'Dollar({})'.format(self.value)
def __str__(self):
return '${:.2f}'.format(self.value)
>>> d1 = Dollar(1.2)
>>> d2 = Dollar(4.63)
>>> d1
Dollar(1.2)
>>> str(d1)
'$1.20'
>>> d2
Dollar(4.63)
>>> str(d2)
'$4.63'
>>> d1+d2
Dollar(5.83)
>>> d1-d2
Dollar(-3.43)
>>> print(d1+d2)
$5.83
>>> print(d1-d2)
$-3.43
>>> price = Dollar(0.99)
>>> 'The price is {}'.format(price)
'The price is $0.99'Do you see the difference? ;)
Also there is alot more, but for the beginning is that enough.
More on reddit.comSee String Formatting Operations:
%d is the format code for an integer. %f is the format code for a float.
%s prints the str() of an object (What you see when you print(object)).
%r prints the repr() of an object (What you see when you print(repr(object)).
For a float %s, %r and %f all display the same value, but that isn't the case for all objects. The other fields of a format specifier work differently as well:
>>> print('%10.2s' % 1.123) # print as string, truncate to 2 characters in a 10-place field.
1.
>>> print('%10.2f' % 1.123) # print as float, round to 2 decimal places in a 10-place field.
1.12
Try the following:
print "First is: %f" % (first)
print "Second is: %f" % (second)
I am unsure what answer is. But apart from that, this will be:
print "DONE: %f DIVIDED BY %f EQUALS %f, SWEET MATH BRO!" % (first, second, ans)
There's a lot of text on Format String Specifiers. You can google it and get a list of specifiers. One thing I forgot to note:
If you try this:
print "First is: %s" % (first)
It converts the float value in first to a string. So that would work as well.