Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Okay.
Firstly, your example which does not work either, is different from the first example. The first example was
$number=1234567; # with commas, should be "1,234,567" $number =~ s/(\d)(?=(\d{3})+(\D|$))/$1\,/g;
Which will result in the string 1,2,3,4,5,6,7; Not quite what we were looking for.

Your second example both extends the string and adds a decimal portion, and does not work either.

$number='1234567890.01'; $number =~ s/(\d)(?=(\d{3})+(\D|$))/$1\,/g;
Resulting in 1,234,567,890.0,1; You will notice the comma in the decimal portion of the resulting string. I find it strange the the output you presented in your message is correct. Did you paste it from a run, or transcribe it and introduce the alteration in the result by accident? As pointed out by Anon, if you introduce a few more decimal places you will end up with more commas in the decimal portion.
$number='1234567890.0123456789'; # with commas, should be "1,234,567,8 +90.01 $number =~ s/(\d)(?=(\d{3})+(\D|$))/$1\,/g;
Resulting in 1,234,567,890.0,1,2,3,4,5,6,7,8,9.

The problem comes in because we are testing against \D|$ to determine when to place to commas. This allows commas to be placed in the decimal portion. I don't have a great solution but you can do something like the following:

$number='1234567890'; # with commas, should be "1,234,567,890.01 $number .= '.' if ($number !~ /\./); # Make sure we end with a decimal + if we don't have a decimal $number =~ s/(\d)(?=(\d{3},?)+(\.))/$1\,/g; chop $number if ($number =~ /\.$/); # Get rid of that useless trailing + decimal print "N $number \n"; $number='1234567890.01234567687'; # with commas, should be "1,234,567, +890.01 $number .= '.' if ($number !~ /\./);# Make sure we end with a decimal +if we don't have a decimal $number =~ s/(\d)(?=(\d{3},?)+(\.))/$1\,/g; chop $number if ($number =~ /\.$/); # Get rid of that useless trailing + decimal print "N $number \n";
The are better solutions presented, I just wanted to respond to why this example did not work.

In reply to Re: Re: Re: Re: Separating big numbers with commas by Sifmole
in thread Separating big numbers with commas by Legg83

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 lurking in the Monastery: (2)
As of 2024-04-20 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found