|
ru.perl- RU.PERL ---------------------------------------------------------------------- From : Anton Petrusevich 2:5004/16.29 03 Aug 2000 09:36:25 To : Pavel Zaikin Subject : Re: print число -------------------------------------------------------------------------------- >>>>> "PZ" == Pavel Zaikin writes: PZ> Здравствуй, All! Я хочу напечатать число с пробелами через каждые 3 PZ> цифры. Типа : $s=1234567; => 1 234 567. PZ> пока я сделал так : @d=split(//,$s); print "$d[-8]$d[-7] PZ> $d[-6]$d[-5]$d[-4] $d[-3]$d[-2]$d[-1]\n" ; PZ> можно это сделать как-то по другому ? perldoc -q comma =head1 Found in /usr/lib/perl5/5.00503/pod/perlfaq5.pod =head2 How can I output my numbers with commas added? This one will do it for you: sub commify { local $_ = shift; 1 while s/^([-+]?\d+)(\d{3})/$1,$2/; return $_; } $n = 23659019423.2331; print "GOT: ", commify($n), "\n"; GOT: 23,659,019,423.2331 You can't just: s/^([-+]?\d+)(\d{3})/$1,$2/g; because you have to put the comma in and then recalculate your position. Alternatively, this commifies all numbers in a line regardless of whether they have decimal portions, are preceded by + or -, or whatever: # from Andrew Johnson <ajohnson@gpu.srv.ualberta.ca> sub commify { my $input = shift; $input = reverse $input; $input =~ s<(\d\d\d)(?=\d)(?!\d*\.)><$1,>g; return scalar reverse $input; } -- Best regards, Anton. --- Gnus v5.5/XEmacs 20.4 - "Emerald" * Origin: My Own Server at Omskelecom (2:5004/16.29@fidonet) Вернуться к списку тем, сортированных по: возрастание даты уменьшение даты тема автор
Архивное /ru.perl/16090830d339f.html, оценка из 5, голосов 10
|