I remember that book... you're making a custom template filter right? You need to convert the value being passed in to locale.currency from a string to an int/decimal/float.
Best practice to avoid rounding errors with currency is to use the decimal package.
Import the decimal package and pass your value through the Decimal function to fix the issue.
from decimal import Decimal
value = Decimal(value)
So your code should look like this:
from django import template
import locale
from decimal import Decimal
register = template.Library()
@register.filter(name='currency')
def currency(value):
try:
locale.setlocale(locale.LC_ALL,'en_US.UTF-8')
except:
locale.setlocale(locale.LC_ALL,'')
value = Decimal(value)
loc = locale.localeconv()
return locale.currency(value, loc['currency_symbol'], grouping=True)
Answer from Higgs Bogson on Stack OverflowI remember that book... you're making a custom template filter right? You need to convert the value being passed in to locale.currency from a string to an int/decimal/float.
Best practice to avoid rounding errors with currency is to use the decimal package.
Import the decimal package and pass your value through the Decimal function to fix the issue.
from decimal import Decimal
value = Decimal(value)
So your code should look like this:
from django import template
import locale
from decimal import Decimal
register = template.Library()
@register.filter(name='currency')
def currency(value):
try:
locale.setlocale(locale.LC_ALL,'en_US.UTF-8')
except:
locale.setlocale(locale.LC_ALL,'')
value = Decimal(value)
loc = locale.localeconv()
return locale.currency(value, loc['currency_symbol'], grouping=True)
From the python docs.
abs()
Return the absolute value of a number. The argument may be a plain or long integer or a floating point number. If the argument is a complex number, its magnitude is returned.
The error raised suggest you are passing a str argument to the abs() method where as it expects the choice of above mentioned arguments. Solution would be to explicitly pass an integer object to the abs method.
Example:
>>>number = "1"
>>>print abs(int(number))
1
>>>
[Python 3.4] Making a simple program to print the absolute value of integer but coming up with an error
TypeError: bad operand type for abs(): 'str'
python - Variable comes up as a string; TypeError: bad operand type for abs(): 'str' - Stack Overflow
Checking the type of data
print("Enter a integer:") var1=input() print(abs(var1))
this is the error I get when I run it
TypeError: bad operand type for abs(): 'str'
X variable wasn't updated when you converted it to int. It should be x = int(x).
import time
x = 0
while True:
print("")
x = input("Please give a value for X.")
try:
x = int(x)
except:
print("")
print("Sorry, please use an integer and try again!")
time.sleep(1)
else:
x = int(x)
break
print(abs(x))
Do:
abs(int(x))
To convert x to integer again
Hey guys. I'm currently learning Python and yesterday I ran into my first hurdle. This is the practice problem:
Create a function, called distance_from_zero(), that returns the absolute value of a provided single argument and prints a statement "Not possible" if the argument provided is not a number. To solve the task, use the type() function in the body of distance_from_zero(). Call the function with the values of -10 and "cat" to verify it works correctly.
Here is the code that I created:
def distance_from_zero(d):
if type(d) == float or int:
return abs(d)
else:
print("Not possible")Whenever I input an integer or float to test the code, it works. However, when I test for strings, it does not print "Not possible" but instead throws an error. This is the error that it produces:
TypeError Traceback (most recent call last)
Cell In[6], line 1 ----> 1 distance_from_zero("asd")
Cell In[2], line 3, in distance_from_zero(d) 1 def distance_from_zero(d): 2 if type(d) == float or int: ----> 3 return abs(d) 4 else: 5 print("Not possible")
TypeError: bad operand type for abs(): 'str'
In my understanding, the "if type(d) == float or int:" should produce a false boolean response which should cause it to print "Not possible", but instead, I'm getting an error. Hopefully someone can point out my mistake or where I understood incorrectly? Thank you in advance!