Solved

adding a comma to numeric value xml/perl

Posted on 2007-03-29
5
203 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now