Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Thousand Separator to number in Javascript / Python / Django

I wanted to ask how to add thousand separator to the number when I type the number and also to the output.

For example 10 000 becomes 10,000.

I tried to use Django intcomma but it's not working.

Really appreciate if you guys can help me with this. Below is my code :

HTML

<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>

<body>
  <div class="container">
    <br />
    <br />
    <div class="form-group">
      <form name="add_price" id="add_price">
        <div class="table-responsive">
          <table class="table table-bordered" id="price">
            {{ priceformset.management_form }}
            {% for price in priceformset %} 
            <tr>
              <td>{{ product.product_price }}</td>
              <td>{{ product.product_quantity }}</td>

              <td>{{ product.product_total_price }}</td>
            </tr>
            {% endfor %}
          </table>
          <input type="button" name="submit" id="submit" class="btn btn-info" value="Submit" />
        </div>
      </form>
    </div>
  </div>
</body

Javascript

$('.textInput').on('change keyup', function() {

      product_total_price=0;

      var product_price= Number($('#id_Product-0-price').val());
      var product_quantity= Number($('#id_Product-0-quantity').val());

      product_total_price= product_price * product_quantity;
      $('#id_Product-0-total_price').val(product_total_price);

});

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__')
like image 297
jojo1711 Avatar asked Mar 31 '26 03:03

jojo1711


2 Answers

javascript:

const n = 100000
const formated = n.toLocaleString()

python (requires python 3.6+):

n = 1000000
formatted = f'{n:,}'

It's easy to do in js:

let number = 1000
number.toLocaleString()

This is how to do it in python:

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

print(place_value(1000000)) 

Happy to help

like image 21
thealpha93 Avatar answered Apr 02 '26 19:04

thealpha93