Closed Thread
Results 1 to 5 of 5

Thread: I don't know much..

  1. #1
    NeedHelp Guest

    I don't know much..

    I don't know much about perl and have never really used it. I've got to fix a program though and here is what I need:

    The number I get now looks like: 1234567
    I need to add two decimals and commas to that number so that it looks like: 12,345.67

    Does anyone know how to do this?

  2. CODECALL Circuit advertisement

     
  3. #2
    KevinADC is offline Programmer
    Join Date
    Jan 2007
    Posts
    125
    Rep Power
    0
    if you can assume the last two digits will always will be the decimal part (or the cents):

    Code:
    my $d = 1245678;
    $d =~ s/(\d{2})$/\.$1/; 
    $d =~ s/\G(\d{1,3})(?=(?:\d\d\d)+(?:\.|$))/$1,/g;
    print $d;

  4. #3
    NeedHelp Guest
    Thanks! That worked like a charm!

  5. #4
    Void's Avatar
    Void is offline Programming Expert
    Join Date
    Jun 2006
    Posts
    410
    Rep Power
    23
    You could always just divide by 100

    123232 / 100 = 1232.32
    Void

  6. #5
    KevinADC is offline Programmer
    Join Date
    Jan 2007
    Posts
    125
    Rep Power
    0
    Quote Originally Posted by Void View Post
    You could always just divide by 100

    123232 / 100 = 1232.32
    Not really, at the least that will not add commas.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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