Professional Web Applications Themes

Number formatting - PERL Beginners

Hi all, I am doing conversions from square metres to square feet for example. I have a regex that removes any commas or spaces in a number. I then do the conversion return an integer but then want to display the number with the commas or spaces put back in to make it easier to read. What do you suggest? my $units = iwpt_dcr_value('dcr.lettableArea.units'); my $area = iwpt_dcr_value('dcr.lettableArea.Area'); $modifiedArea =~ s/,\s//; $squareMetres = ( $units eq 'square metres' ) ? $area : int $modifiedArea * 0.0929; $squareFeet = ( $units eq 'square feet' ) ? $area : int $modifiedArea * ...

  1. #1

    Default Number formatting

    Hi all,

    I am doing conversions from square metres to square feet for example.

    I have a regex that removes any commas or spaces in a number.

    I then do the conversion

    return an integer

    but then want to display the number with the commas or spaces put back in
    to make it easier to read.

    What do you suggest?


    my $units = iwpt_dcr_value('dcr.lettableArea.units');
    my $area = iwpt_dcr_value('dcr.lettableArea.Area');

    $modifiedArea =~ s/,\s//;

    $squareMetres = ( $units eq 'square metres' ) ? $area :
    int $modifiedArea * 0.0929;
    $squareFeet = ( $units eq 'square feet' ) ? $area
    : int $modifiedArea * 10.764;

    $lettableArea = $squareMetres .qq[m<sup>2</sup> or ] .
    $squareFeet . qq[f<sup>2</sup> (approx)];

    iwpt_output( $lettableArea );

    Regards

    Colin
    Colin Guest

  2. #2

    Default Re: Number formatting

    On Thu, 5 Aug 2004 com.au wrote:
     

    Two minutes of Googling found dozens of suggestions.

    This may be the best one:

    How can I output my numbers with commas added?

    This subroutine will add commas to your number:

    sub commify {
    local $_ = shift;
    1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
    return $_;
    }

    This regex from Benjamin Goldberg will add commas to numbers:

    s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1,/g;

    It is easier to see with comments:

    s/(
    ^[-+]? # beginning of number.
    \d{1,3}? # first digits before first comma
    (?= # followed by, (but not included in the match) :
    (?>(?:\d{3})+) # some positive multiple of three digits.
    (?!\d) # an *exact* multiple, not x * 3 + 1 or whatever.
    )
    | # or:
    \G\d{3} # after the last group, get three digits
    (?=\d) # but they have to have more digits after them.
    )/$1,/xg;

    <http://search.cpan.org/~nwclark/perl-5.8.5/pod/perlfaq5.pod#How_can_I_output_my_numbers_with_comm as_added?>


    Google and PerlFaq are your friends.


    --
    Chris Devers com
    http://devers.homeip.net:8080/blog/

    np: 'Mr. Lucky'
    by Henry Mancini
    from 'The Best Of Mancini'
    Chris Guest

  3. #3

    Default Re: Number formatting

    On 8/4/2004 10:02 PM, com.au wrote:
     

    Number::Format


    Randy Guest

Similar Threads

  1. phone number formatting
    By T. in forum FileMaker
    Replies: 8
    Last Post: January 22nd, 01:13 PM
  2. phone number formatting
    By Shay in forum PHP Development
    Replies: 4
    Last Post: January 18th, 05:32 PM
  3. Number formatting?
    By Robert Cowham in forum Ruby
    Replies: 4
    Last Post: October 30th, 04:36 AM
  4. Number formatting in CDML
    By Matt Revenaugh in forum FileMaker
    Replies: 1
    Last Post: July 23rd, 12:06 AM
  5. Formatting a number
    By Rick Brandt in forum Microsoft Access
    Replies: 4
    Last Post: July 15th, 08:55 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139