I read a lot on this topic. There are solutions for this mater. The most promising involves a module named Number::Format. I found also regexp based solutions, which gave me a headache and which I could not get to work properly. Finally there are millions of fancy code bits around for this purpose. There is also one solution to forget definitively: sprintf!

The solution I am posting here is thought for web-application. Web-application written in PERL have performance issues and unfortunately performance, meaning user perceived response times, are THE acceptance factor for your application. To solve this issue we fortunately have a wonderful Apache module named mod_perl.

Mod_perl can be tuned by loading modules into memory with the PerlModule directive. However you can not load infinitely modules in memory, since it is a scarce resource (more information on http://perl.apache.org/docs/1.0/guide/performance.html). Therefore I discarded the Number::Format module solution, which holds much more functions than what I need.

A web-application can have two layers of validation and formating. The first layer being the browser, the second being the PERL code on the server. The best is of course to validate and format your data at both layer. Therefor I was looking for a solution in JavaScript and one in PERL, which would be similar. So I realized them...

Here the JavaScript function:

<script type='text/javascript'> function formatNumber(myElement) { // JavaScript function to inser +t thousand separators var myVal = ""; // The number part var myDec = ""; // The digits pars // Splitting the value in parts using a dot as decimal separat +or var parts = myElement.value.toString().split("."); // Filtering out the trash! parts[0] = parts[0].replace(/[^0-9]/g,""); // Setting up the decimal part if ( ! parts[1] && myElement.value.indexOf(".") > 1 ) { myDec += ".00" } if ( parts[1] ) { myDec = "."+parts[1] } // Adding the thousand separator while ( parts[0].length > 3 ) { myVal = "'"+parts[0].substr(parts[0].length-3, parts[0].le +ngth )+myVal; parts[0] = parts[0].substr(0, parts[0].length-3) } myElement.value = parts[0]+myVal+myDec; } </script> <!-- in the html page --> <input name="amount" id="amount" type="text" onkeyup="formatNumber(thi +s);">

The same function in PERL

sub thousandformat { # Formats number with thousand separators my ($number) = @_; my $currnb = ""; my ($mantis, $decimals) = split(/\./, $number, 2); $mantis =~ s/[^0-9]//g; while ( length($mantis) > 3 ) { $currnb = "'".(substr($mantis, length($mantis)-3, 3 )).$currnb +; $mantis = substr($mantis, 0, length($mantis)-3); } $currnb = $mantis.$currnb; if ( $decimals ) { $currnb .= ".".$decimals; } else { $currnb .= ".00"; } return $currnb; }

Enjoy

K

The best medicine against depression is a cold beer!

In reply to Formating numbers with thousand separator - Solution for web-applications by Zzenmonk

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.