Why Do We Want To Do This?

  • I've written some code in Perl that takes a number and basically converts it to a formatted number, with a specified precision after the decimal point (similar to sprintf()).
  • This code was converted to Javascript (try not to laugh too hard lol!)
  • What we have so far is fine, but I'd like to see if there's anyone out there that knows how to make the regular expression even more cryptic or reduce the number of lines of code we have!

    The Perl Regular Expressions

    # Pad the number with a bunch of zeros $strValue =~ s/^(\d*)\.?(\d*)$/${1}.${2}000000000/; # Remove all but the first '$strPrecision' digits that immediately # trail the decimal point. $strDefaultPrecision = 2; $strPrecision = ($strPrecision >= 0) ? $strPrecision : $strDefaultPrec +ision; if ($strPrecision == 0) { $strValue =~ s/^(\d*)\.?\d*$/$1/; } else { $strValue =~ s/^(\d*\.\d{$strPrecision})\d*$/$1/; }

    The JavaScript Code

    function ConvertNumberToCurrency (strValue, strPrecision) { /* Takes a string that contains a number with optional decimal point an +d returns the number with 2 decimal places. An optional precision parameter is provided. Assumptions: - strValue cannot contain any non-numeric characters (0-9 and .), or + this will not work. - strPrecision must be numeric. - this function works to only 10 digits of accuracy. Usage: ConvertNumberToCurrency (""); ConvertNumberToCurrency (".5"); ConvertNumberToCurrency ("1"); ConvertNumberToCurrency ("3."); ConvertNumberToCurrency ("100", "4"); ConvertNumberToCurrency ("12.4", 1); ConvertNumberToCurrency ("12.56", 0); ConvertNumberToCurrency ("12.46435"); */ var strDefaultPrecision = 2; var regexTruncate; strValue += ""; // Convert any digits to string if (! strValue) { strValue = "0"; } // Convert empty string to 0 // Set any non-numeric values to 0 if (! strValue.match (/^\d+\.?$|\d+\.?\d*$|\d*\.?\d+$/)) { strValue += "0"; } // Add a leading 0 to any numbers starting with a decimal point if (strValue.match (/^\./)) { strValue = "0" + strValue; } // Set the default precision if no precision is provided. strPrecision = (strPrecision >= 0) ? strPrecision : strDefaultPrecis +ion; // Pad the number with a bunch of zeros strReplaceValue = "$1.$2" + "0000000000"; strValue = strValue.replace ( /^(\d*)\.?(\d*)$/, strReplaceValue); // Remove all but the first 'strPrecision' digits that immediately // trail the decimal point.. if (strPrecision == 0) { regexTruncate = new RegExp ("^(\\d*)\\.?\\d*$"); } else { regexTruncate = new RegExp ("^(\\d*\\.\\d{"+strPrecision+"})\\d*$" +); } strValue = strValue.replace (regexTruncate, "$1"); return (strValue); }

    The Question

    Does anyone see a way to make the Perl regular expression(s) or algorithm:
  • more efficient
  • in fewer lines
  • in one regex
    which we will then port over to the JavaScript function shown above? If you've got an idea, hit me with it!


    In reply to Using Regular Expressions to Simulate sprintf() function - One Liner Challenge by Incognito

    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.