I have been playing around with Template::Toolkit for some time now and I have run into the need of currency formatting. I currently have a commafy display function (the code for that I found here :-D) that I am using in my Template and I tried using the format filters that come with Template::Toolkit to add decimal places to the number, but it appears that it only effects the output.

It would be ideal to have a solution that I can format the number and then commafy it via Template::Toolkit and not have to inject perl code (which I'm doing now via sprintf.) I have a couple of ideas on how to do this and I am seeking wisdom on the best approach.

  1. I could add another function to my display module that will take care of adding decimal places to my number. In my template I would call the decimal format function first and then the commafy function to get the desired output.
  2. I could create a new function that commafys and formats the number, but just because I'm adding decimal places I might not want to commafy and just because I want to commafy a number doesn't mean that I want to add decimal places.
  3. I could format the number in my template driver module and I wouldn't have to worry about formatting in my template, but I know that the template is where formatting of data should occur, so this option isn't my most ideal solution.

Any suggestions on how to get this done the right and correct way would be greatly appreciated. Thank you for your time.


In reply to Template Toolkit Formatting Data Question (sprintf and commafy) by Tyraziel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.