Search code examples
javascriptformatdecimalcurrency

format getting rounded off by a large amount using Intl.NumberFormat


let just_the_number = '8973565';

const just_the_number_formatted = Intl.NumberFormat("en-IN", { minimumFractionDigits: 0 }).format(parseFloat(just_the_number));
console.log(just_the_number_formatted)

let formatted_amount = new Intl.NumberFormat("en-IN", {
      style: "currency",
      currency: "INR",
      notation: "compact",
      compactDisplay: "long",
    }).format(just_the_number);

console.log(formatted_amount) // prints ₹90L

formatted_amount is getting rounded off by a large amount here. I want the above printed ₹89.73L


Solution

  • You have to use the minimumFractionDigits and maximumFractionDigits options to customize behaviour for decimal places.

    Note that it will still get rounded.

    For example:

    let just_the_number = '8973565';
    
    let formatted_amount = new Intl.NumberFormat("en-IN", {
          style: "currency",
          currency: "INR",
          notation: "compact",
          compactDisplay: "long",
          minimumFractionDigits: 2,
          maximumFractionDigits: 2,
        }).format(just_the_number);
    
    console.log(formatted_amount) // prints ₹89.74L