Solved

adding a comma to numeric value xml/perl

Posted on 2007-03-29
5
206 Views
Last Modified: 2010-04-16
I have an xml variable in a script that hols a value like 2084. I want to convert it into 2,084. (put a comma in it) How do I do it?  Using Perl?

Thanks.
0
Comment
Question by:anup001
5 Comments
 
LVL 48

Accepted Solution

by:
Tintin earned 300 total points
ID: 18821257

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

0
 

Author Comment

by:anup001
ID: 18821295
what is the input variable here?
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 300 total points
ID: 18821355
$num is the variable.

So if you have

my $num=2084;
$num =~ s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1,/g;
print "Num is $num\n";

The output will be

Num is 2,084

0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 150 total points
ID: 18821739
perldoc -q "How can I output my numbers with commas added"
Found inperlfaq5.pod
       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;
0
 
LVL 17

Assisted Solution

by:mjcoyne
mjcoyne earned 50 total points
ID: 18822468
Yet another method:
#!/usr/bin/perl -w
use strict;

my $number = "1234567.00";
$number = commify($number);

print "$number\n";

sub commify {
    my $input = shift;
    $input = reverse $input;
    $input =~ s<(\d\d\d)(?=\d)(?!\d*\.)><$1,>g;
    $input =  reverse $input;
    return $input;
}
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question