DEV Community

Cover image for How to Format Indian Currency in JavaScript (Lakhs & Crores Guide)
M Rahman
M Rahman

Posted on

How to Format Indian Currency in JavaScript (Lakhs & Crores Guide)

Agar aap koi Indian e-commerce store, billing dashboard ya SaaS application build kar rahe hain, to currency formatting ek aisi cheez hai jisme developers aksar fas jaate hain.

Default roop se, zyadatar programming languages aur libraries Western numbering system follow karti hain, jahan har 3 digits ke baad comma lagta hai (thousands, millions, billions). Lekin India me standard numbering system alag chalta hai: pehle 3 digits, aur uske baad har 2 digits ke baad comma lagta hai (Thousands, Lakhs, Crores).

Example:

  • Western System: 1,234,567.89 (1.23 Million)
  • Indian System: 12,34,567.89 (12.34 Lakhs)

Is guide me hum dekhenge ki JavaScript aur TypeScript me Indian currency ko properly format karne ke kaunse alag-alag tareeqe hain aur unke pros/cons kya hain.

1. Using Native Intl.NumberFormat (Built-in Web API)

Modern JavaScript me ek powerful built-in tool hota hai: Intl.NumberFormat. Iske zariye aap locale-based currency styling easily apply kar sakte hain.

const amount = 1234567.89;

const formattedINR = new Intl.NumberFormat('en-IN', {
  style: 'currency',
  currency: 'INR',
  maximumFractionDigits: 2,
}).format(amount);

console.log(formattedINR);
// Output: ₹12,34,567.89

Enter fullscreen mode Exit fullscreen mode

Advantages:

  • Zero dependencies: Kisi external library ki zaroorat nahi padti.
  • Browser me natively supported hai.

Limitations:

  • Server-Side Inconsistencies: Node.js ke purane runtimes ya minimal docker images me agar full ICU data installed nahi hai, to locale fallback ho kar standard commas dikha deta hai.
  • No Built-in Number-to-Words: Cheques ya invoices ke liye number ko words me convert karne ka feature isme nahi hota.
  • Compact Notation Issues: Jab aap compact form (12 Lakh, 2.5 Crore) banana chahte hain, to default behavior aksar confuse karta hai ya Western symbols (M, B) me fallback kar jaata hai.

2. Using Custom Regex Logic (Pure JS)

Agar aapko kisi heavy API par depend nahi hona aur pure regex se format karna hai, to standard Vedic format ka custom function kuch is tarah likha jata hai:

function formatIndianCurrency(amount) {
  if (isNaN(amount) || amount === null) return '';

  const parts = Number(amount).toFixed(2).split('.');
  let integerPart = parts[0];
  const decimalPart = parts[1];

  // Pehle aakhiri 3 digits extract karein, fir bache hue par 2-digit groups banayein
  const lastThree = integerPart.substring(integerPart.length - 3);
  const remaining = integerPart.substring(0, integerPart.length - 3);

  let formatted = remaining.replace(/\B(?=(\d{2})+(?!\d))/g, ',');
  formatted = remaining.length > 0 ? `${formatted},${lastThree}` : lastThree;

  return `₹${formatted}.${decimalPart}`;
}

console.log(formatIndianCurrency(1234567.89));
// Output: ₹12,34,567.89
Enter fullscreen mode Exit fullscreen mode

Limitations:

  • Regex code padhne aur maintain karne me mushkil hota hai.
  • Negative numbers aur rounding edge cases handle karne ke liye extra code likhna padta hai.

3. The Clean & Modern Way: india-formatter

Agar aapko ek lightweight, production-ready solution chahiye jisme proper TypeScript autocomplete ho aur jo browser se lekar Next.js Server Components tak har jagah ek jaisa kaam kare, to aap india-formatter use kar sakte hain.

Yeh ek zero-dependency (<2KB) package hai jo specially Indian fintech aur billing requirements ke liye design kiya gaya hai.

Installation

npm install india-formatter
Enter fullscreen mode Exit fullscreen mode

Usage Examples

1. Standard Vedic Comma Formatting

import { formatINR } from 'india-formatter';

formatINR(1234567.89);
// => "₹12,34,567.89"

// Custom options: decimals control aur custom prefix
formatINR(150000, { decimals: 0 });
// => "₹1,50,000"

formatINR(150000, { showSymbol: false, decimals: 0 });
// => "1,50,000"
Enter fullscreen mode Exit fullscreen mode

2. Compact Representation (Lakhs & Crores)

Dashboard cards aur mobile screens par bade numbers ko compact dikhana zaroori hota hai:

import { formatINRCompact } from 'india-formatter';

formatINRCompact(1500000);
// => "₹15 Lakh"

formatINRCompact(25000000);
// => "₹2.5 Crore"
Enter fullscreen mode Exit fullscreen mode

3. Invoices & Receipts ke liye Number to Words

Billing software me invoice total ko words me print karna mandatory compliance hota hai:

import { toWordsINR } from 'india-formatter';

toWordsINR(125000.50);
// => "One Lakh Twenty-Five Thousand Rupees and Fifty Paise Only"
Enter fullscreen mode Exit fullscreen mode

4. Built-in Regulatory & KYC Validators

Currency formatting ke alawa Indian apps me form validation ki zaroorat hoti hai:

import { isValidPAN, isValidGSTIN, isValidIndianPhone } from 'india-formatter';

isValidPAN('ABCDE1234F');        // true
isValidGSTIN('27ABCDE1234F1Z5'); // true
isValidIndianPhone('+91 9876543210'); // true
Enter fullscreen mode Exit fullscreen mode

Summary: Kise Kab Use Karein?

Requirement - Best Solution

Simple browser-only display - Intl.NumberFormat('en-IN')

Full Stack (Node, Next.js, Edge) with zero config - india-formatter

Invoice Number-to-Words & Compact Scales - india-formatter

PAN / GSTIN / Phone checks in the same app - india-formatter

Agar aap apne codebase ko clean aur reliable rakhna chahte hain, to wheel reinvent karne ke bajay ek well-tested micro-package use karna hamesha safe rehta hai.

Aap apne projects me currency handling kaise karte hain? Comments me apna experience zaroor share karein!

Top comments (1)

Collapse
 
ethanmoorenotes profile image
Ethan Moore •

Good point about Intl.NumberFormat falling back to Western grouping when Node runs without full ICU data. I've hit that in a slim Docker image, where en-IN output differed between local and CI, and a quick unit test asserting '12,34,567.89' caught it early. Have you compared how formatToParts behaves for building the lakh/crore compact labels?