DEV Community

SEO - NT Tech Innovation
SEO - NT Tech Innovation

Posted on Edited on

A Tiny Rupee Formatter for SMM Price Lists: Lakhs, Crores and Per-1000 Rates in JavaScript

If you build a price page for an Indian audience, two small things trip people up. Indian digit grouping (12,34,567 and not 1,234,567) and per-1000 pricing, which is how most SMM panels quote their rates.

Both are easy once you stop hand-rolling string code.

Indian grouping with Intl.NumberFormat

No library needed. The built-in formatter knows the lakh and crore grouping.

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

console.log(inr.format(1234567.5)); // ₹12,34,567.50
console.log(inr.format(39));        // ₹39.00
Enter fullscreen mode Exit fullscreen mode

Per-1000 rates without float surprises

A panel lists a service at, say, ₹39 per 1000. A user orders 2,350. The cost is rate × quantity ÷ 1000. Do the maths in paise so you never show ₹91.64999.

function costInPaise(ratePer1000, qty) {
  const ratePaise = Math.round(ratePer1000 * 100);
  return Math.round((ratePaise * qty) / 1000);
}

function showCost(ratePer1000, qty) {
  return inr.format(costInPaise(ratePer1000, qty) / 100);
}

console.log(showCost(39, 2350)); // ₹91.65
Enter fullscreen mode Exit fullscreen mode

Guard the quantity

Every service has a minimum and a maximum. Check before you calculate.

function checkQty(qty, min, max) {
  if (!Number.isInteger(qty)) return 'Quantity must be a whole number';
  if (qty < min) return 'Minimum order is ' + min;
  if (qty > max) return 'Maximum order is ' + max;
  return null;
}
Enter fullscreen mode Exit fullscreen mode

Where the numbers come from

I tested the layout against a real public price list. The SMMYodha services page shows rupee rates per 1000 with min and max columns, so it was a handy reference for edge cases. SMMYodha is an Indian SMM panel, and its table format is typical of the category.

Takeaways

  • Use en-IN with Intl.NumberFormat for lakh and crore grouping.
  • Store and calculate money as integers.
  • Validate min and max before showing a price.

That's the whole thing. About 20 lines and no dependencies.

Top comments (0)