Europen numbers

I will admit I asked ChatGPT to help. It works pretty well as a starter:

// #popclip
// name: Money Swap
// icon: iconify:noto-v1:money-mouth-face
// language: javascript

/**
 * Convert between Australian (1,234.56) and European (1.234,56)
 * number formats by swapping dots and commas appropriately.
 */
function swapMoneyFormat(text) {
  // Trim whitespace
  text = text.trim();

  // Patterns to detect formats
  const auPattern = /^[0-9]{1,3}(?:,[0-9]{3})*(?:\.[0-9]+)?$/;
  const euPattern = /^[0-9]{1,3}(?:\.[0-9]{3})*(?:,[0-9]+)?$/;

  if (auPattern.test(text)) {
    // AU → EU
    let [intPart, decPart] = text.split('.');
    // Replace thousands commas with dots
    intPart = intPart.replace(/,/g, '.');
    // Join with comma as decimal separator if present
    return decPart != null ? `${intPart},${decPart}` : intPart;
  }

  if (euPattern.test(text)) {
    // EU → AU
    let [intPart, decPart] = text.split(',');
    // Replace thousands dots with commas
    intPart = intPart.replace(/\./g, ',');
    // Join with dot as decimal separator if present
    return decPart != null ? `${intPart}.${decPart}` : intPart;
  }

  // If it doesn't match either, leave unchanged
  return text;
}

popclip.pasteText(swapMoneyFormat(popclip.input.text));

(The above block is an extension snippet — select it then click “Install Extension” in PopClip.)

2 Likes