Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-19 14:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found