preloader
Formatting numbers according to Indian Locale in Javascript and Python

Formatting numbers according to Indian Locale in Javascript and Python

I came across this problem while building an app recently and thought it might be interesting to share. The problem we are trying to solve is of displaying numbers according to locale. Eg. in india, number 100000 is written as 1,00,000 while in US it is written as 100,000. So, how can we write a program to format numbers properly according to the locale in Javascript and Python is the question we are trying to answer.

Brute force way!!

The raw or brute force way for doing this is to write a program from scratch. However, to be able to handle different locales we will have to come up with a pretty non-trivial program. Since I was concentrating on Indian locale (100000 as 1,00,000 etc.), I came up with a simple program for doing the same in Javascript.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
function format(number){
    let formattedNumber = ''
    let i=0;
    if(number.length%2==0){
        formattedNumber += number[i++]+","
    }
    //place ,(comma) every two digits
    for(; i<number.length-3; i+=2){
        formattedNumber += number[i]+number[i+1]+","
    }
    //join in remaining digits
    while(i<number.length){
        formattedNumber += number[i++]
    }
    return formattedNumber
}

let givenNumber = '100000' //read the number as a string
//if number has less than 4 digits display as it is
console.log((givenNumber.length < 4)?givenNumber:format(givenNumber))

The logic here is pretty straightforward. If the number of digits is even then we append the first digit followed by a comma and append comma every third digit. Else, we just append comma every third digit. Of course, there are lots of other ways to do the same. However, the preferred way is to just use the builtin internationalization module (Intl) in JS.

1
2
let number = 100000
console.log(new Intl.NumberFormat('en-IN').format(number)); //1,00,000

We can customize the output even more by passing in options argument and also locales. Since I was mainly interested in Indian locale I ve used “en-IN”. For more information please read Intl MDN docs Python too has a module called locale.

1
2
3
4
import locale
locale.setlocale(locale.LC_NUMERIC, "en_IN")
number = 100000
print(f"{number:n}")

As you can see both python and javascript have quite good support for internationalization.

You may be interested in some of our courses

Python For Data Science
  • 01 Month
  • Programming

Python For Data Science

Improve programming skills through live coding solution for programming puzzles and application …

View Details
Building cloud ready apps with Golang
  • 01 Month
  • Programming

Building cloud ready apps with Golang

Go programming language is amongst the most popular languages these days. The only language with …

View Details
C Programming - zero to hero
  • 01 Month
  • Programming

C Programming - zero to hero

Gain deep understanding of programming and computer working through this beginner C Programming …

View Details