skip to Main Content

I want to change the currency format like replace comma(,) with dots and dots with comma. like
22,22,22.00 to 22.22.22,00 currently i am using javascript package name jquery-formatcurrency.js it is doing good for US format but i want another region format like europen format.
I tried to change the package code but it is not working.

$('.formatCurrency').formatCurrency();

4

Answers


  1. You can iterate over each string, and change the one you want, like that:

    var s = "22,22,22.00";
    var next = "";
    for (const c of s) {
       if(c == ".")
          next += ",";
       else if(c == ",")
          next += ".";
       else
          next += c;
    }
    console.log(next);
    Login or Signup to reply.
  2. To format numbers according to a specified locale use the toLocaleString() method

    const number = 222222.00;
    const formatted = number.toLocaleString('de-DE', { style: 'currency', currency: 'EUR' }).replace(/./g, ',').replace(/,/g, '.');
    
    console.log(formatted); 
    Login or Signup to reply.
  3. Use Intl API to format the currency or any other numbers.

    MDN Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat

    Sample from MDN Page:

    const number = 123456.789;
    
    // request a currency format
    console.log(
      new Intl.NumberFormat("de-DE", {
        style: "currency",
        currency: "EUR"
      }).format(
        number,
      ),
    );
    // 123.456,79 €
    
    // the Japanese yen doesn't use a minor unit
    console.log(
      new Intl.NumberFormat("ja-JP", {
        style: "currency",
        currency: "JPY"
      }).format(
        number,
      ),
    );
    // ¥123,457
    
    // limit to three significant digits
    console.log(
      new Intl.NumberFormat("en-IN", {
        maximumSignificantDigits: 3
      }).format(
        number,
      ),
    );
    // 1,23,000
    Login or Signup to reply.
  4. you can use a regular expression..

    let 
      nStr_a = '22,22,22.00'
    , nStr_b =  nStr_a.replace(/(,|.)/g, x=>({',':'.','.':','})[x])
      ;
    console.log(nStr_b);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search