Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Vue Filter to show zeros as dashes

I have a vue filter that I use to display numbers as our local currency (GBP) but I would like to show zero’s as a dash rather than ÂŁ0.00. This is because in a large table with lots of zeros it makes it hard to spot the few items that are NOT zero.

My filter currently looks like this:

Vue.filter("gbp", function (value) {
  // Create our number formatter.
  if (value === null || value === undefined) {
    return value;
  } else {
    var formatter = new Intl.NumberFormat("en-GB", {
      style: "currency",
      currency: "GBP",
      minimumFractionDigits: 2,
    });
    return formatter.format(+value);
  }
});

How can I alter it to "hide" the zeros as dashes?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try adding an extra if to test for 0

Inside that clause, you can return anything you like, e.g. -.


Vue.filter("gbp", function (value) {
  // Create our number formatter.
  if (value === null || value === undefined) {
    return value;
  } else if (value === 0) {
    return "-";
  } else {
    var formatter = new Intl.NumberFormat("en-GB", {
      style: "currency",
      currency: "GBP",
      minimumFractionDigits: 2,
    });
    return formatter.format(+value);
  }
});

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading