skip to Main Content

Hey I am trying to write some code to handle the unformatting of EU and US numbers but I can not get a perfect solution.

For example I would like

1.222.222,89 to be 1222222.89
1,222,222.89 to be 1222222.89

I am using AG GRID and I am pasting numbers to the grid which ag grid is storing as strings.

what I have got so far but will not work with numbers with only a single ‘,’ or ‘.’

let unformattedNumber = params.value as string;

unformattedNumber = unformattedNumber.replace("%", "");
unformattedNumber = unformattedNumber.replace(/ /g, "");

const lastCommaIndex = unformattedNumber.lastIndexOf(",");
const lastPeriodIndex = unformattedNumber.lastIndexOf(".");

if (lastCommaIndex > lastPeriodIndex) {
  unformattedNumber = unformattedNumber.replace(/./g, "");
  unformattedNumber = unformattedNumber.replace(",", ".");
} else if (lastPeriodIndex > lastCommaIndex) {
  unformattedNumber = unformattedNumber.replace(/,/g, "");
}

return parseFloat(unformattedNumber);

3

Answers


  1. Seems like you are on the right track. You can try this out:

    let unformattedNumber = params.value as string;
    
    unformattedNumber = unformattedNumber.replace(/%/g, "").replace(/ /g, "");
    
    // Check if the number is in EU format (e.g., 1.222.222,89)
    if (/^[0-9.]{3,},[0-9]{2}$/.test(unformattedNumber)) {
      unformattedNumber = unformattedNumber.replace(/./g, "").replace(/,/g, ".");
    } else if (/^[0-9,]{3,}.[0-9]{2}$/.test(unformattedNumber)) {
      // Check if the number is in US format (e.g., 1,222,222.89)
      unformattedNumber = unformattedNumber.replace(/,/g, "");
    }
    
    return parseFloat(unformattedNumber);
    
    Login or Signup to reply.
  2. I hope it works how You want it to be! Used JS a long time ago.

    function unformatNumber(number) {
    
      let unformattedNumber = number.replace(/[,]/g, '');
      let lastCommaIndex = number.lastIndexOf(',');
      let amount = number.match(/,/g);
        
      unformattedNumber = unformattedNumber.slice(0, lastCommaIndex- 
      amount.length+1) + '.' + unformattedNumber.slice(lastCommaIndex- 
      amount.length+1, unformattedNumber.length)
     
      return unformattedNumber;
    }
    
    let uNumber = "1,222,222,89"; //OUTPUT 1222222.89
    console.log(unformatNumber(uNumber));
    Login or Signup to reply.
  3. I’m not very good with regex so I used arrays😅. May not be the best practice, but it works:

    function formatNumber(unformattedNumber) {
      const rawNumber = unformattedNumber.split("").filter((char) => {
        return char !== "," && char !== "."
      })
      if (rawNumber.length !== 9) {
        console.log("Wrong number format")
      } else {
        const a = rawNumber.pop()
        const b = rawNumber.pop()
        const formattedNumber = [...rawNumber, ".", a, b].join("")
        console.log(formattedNumber)
      }
    }
    
    formatNumber("1.222.222,89")
    

    I tried shortening the .pop() process by just using splice() but for some reason, it wasn’t working too well on JSFiddle, here’s the code for that if you want it tho:

    function formatNumber(unformattedNumber) {
      const rawNumber = unformattedNumber.split("").filter((char) => {
        return char !== "," && char !== "."
      })
      if (rawNumber.length !== 9) {
        console.log("Wrong number format")
      } else {
        const formattedNumber = rawNumber.splice(7, 0, ".").join("")
        console.log(formattedNumber)
      }
    }
    
    formatNumber("1.222.222,89")
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search