Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

แปลงตัวเลขเป็นรูปแบบสกุลเงินอินเดีย - JavaScript

ใช้ toLocaleString('en-IN', {style: 'currency', currency: 'INR'})

ตัวอย่างโค้ด

const num1 = 1000;
const num2 = 129943;
const num3 = 76768798;
const toIndianCurrency = (num) => {
  const curr = num.toLocaleString('en-IN', {
    style: 'currency',
    currency: 'INR'
  });
  return curr;
};
console.log(toIndianCurrency(num1));
console.log(toIndianCurrency(num2));
console.log(toIndianCurrency(num3));

ผลลัพธ์

₹1,000.00
₹1,29,943.00
₹7,67,68,798.00

คำอธิบาย: ระบบสกุลเงินอินเดียใช้ comma แบบ lakh/crore (หลักพัน, แสน, ล้าน) en-IN locale จัดรูปแบบอัตโนมัติ style: 'currency' + currency: 'INR' ใส่สัญลักษณ์ ₹ และทศนิยม 2 ตำแหน่ง