Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Javascript/Python/Django中向数字添加千位分隔符_Javascript_Python_Django - Fatal编程技术网

如何在Javascript/Python/Django中向数字添加千位分隔符

如何在Javascript/Python/Django中向数字添加千位分隔符,javascript,python,django,Javascript,Python,Django,我想问一下,在我输入数字和输出时,如何在数字中添加千位分隔符 例如,10000变为10000 我尝试使用Django intcomma,但它不起作用 非常感谢你们能帮我。下面是我的代码: HTML Models.py class Price (models.Model): product_price = models.CharField(max_length=512,null=True,blank=True) product_quantity = models.CharFie

我想问一下,在我输入数字和输出时,如何在数字中添加千位分隔符

例如,10000变为10000

我尝试使用Django intcomma,但它不起作用

非常感谢你们能帮我。下面是我的代码:

HTML

Models.py

 class Price (models.Model):
    product_price = models.CharField(max_length=512,null=True,blank=True)
    product_quantity = models.CharField(max_length=512,null=True,blank=True)
    product_total_price= models.CharField(max_length=512,null=True,blank=True)
Forms.py

class PriceForm(forms.ModelForm):

product_price =forms.CharField(required=False,label=('Price'),widget=forms.TextInput(
        attrs= {
        "class":"textInput form-control",
        "placeholder" : "Price"
}))

product_quantity =forms.CharField(required=False,label=('Quantity'),widget=forms.TextInput(
        attrs= {
        "class":"textInput form-control",
        "placeholder" : "Quantity"
}))

product_total_price =forms.CharField(required=False,label=('Total Price'),widget=forms.TextInput(
        attrs= {
        "class":"textInput form-control",
        "placeholder" : "Total Price"
}))

class Meta:
        model = Price
        fields = ('__all__')
在js中很容易做到:

let number = 1000
number.toLocaleString()
以下是如何在python中执行此操作:

def place_value(number): 
    return ("{:,}".format(number)) 

print(place_value(1000000)) 
很高兴为您提供帮助

const n=100000
const formatted=n.tolocalString()
python(需要python 3.6+):

n=1000000
格式化=f'{n:,}'

它也适用于浮式阀。这有点复杂,但我真的很喜欢分享我的想法。它基于Javascript

value.toString().split('.').map((v, i) => i===0?v.split('').map((val, idx, self) => ((idx===0)||((self.length-idx)%3!==0))?val:`,${val}`).join(''):v).join('.')
def place_value(number): 
    return ("{:,}".format(number)) 

print(place_value(1000000)) 
value.toString().split('.').map((v, i) => i===0?v.split('').map((val, idx, self) => ((idx===0)||((self.length-idx)%3!==0))?val:`,${val}`).join(''):v).join('.')