Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

- - - - -

I don't know much..


  • Please log in to reply
4 replies to this topic

#1 Guest_NeedHelp_*

Guest_NeedHelp_*
  • Guest

Posted 20 March 2007 - 09:36 AM

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?
  • 0

#2 KevinADC

KevinADC

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 125 posts

Posted 20 March 2007 - 10:03 AM

if you can assume the last two digits will always will be the decimal part (or the cents):

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

  • 0

#3 Guest_NeedHelp_*

Guest_NeedHelp_*
  • Guest

Posted 20 March 2007 - 10:56 AM

Thanks! That worked like a charm!
  • 0

#4 Void

Void

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 403 posts

Posted 22 March 2007 - 08:45 AM

You could always just divide by 100

123232 / 100 = 1232.32
  • 0
Void

#5 KevinADC

KevinADC

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 125 posts

Posted 22 March 2007 - 11:15 AM

You could always just divide by 100

123232 / 100 = 1232.32


Not really, at the least that will not add commas.
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download