0

I have a collection in Meteor that has an imported csv file with a Zip Code field. The problem is when I print out a document from a query, it will print a zip like 04191 to 4191.

....
{{#each Query}}
<p>{{Zip}</p>
{{/each}}
....

I'd need something like:

....
{{#each Query}}
<p>{{Zip.toString()}</p>
{{/each}}
....
Michel Floyd
  • 18,793
  • 4
  • 24
  • 39
flimflam57
  • 1,334
  • 4
  • 16
  • 31
  • Make sure that that field in the collection is a string anyway, because if it is a number, leading 0 means its an octal number. IN the example you provided it's fine because it cannot be octal and it is read as decimal, but if you take 00333 code for example, it will show "219" as a string – Adam Wolski Jan 05 '16 at 22:22

1 Answers1

1

Here's a generic zip code helper:

 Template.registerHelper('formatZip',function(zip){
   var pad="00000";
   return (pad+zip).slice(-5); // 5 digit zips only!
 });

You can use this from any template in your app with:

{{formatZip Zip}}

Assuming Zip contains the zip code you wish to format.

With props to https://stackoverflow.com/a/9744576/2805154 - this answer merely reformulates that answer for Meteor.

Community
  • 1
  • 1
Michel Floyd
  • 18,793
  • 4
  • 24
  • 39