How to change decimal point ',' to decimal comma ','

R

Reinhard Pagitsch

PERO said:
How to change decimal point ',' to decimal comma ','

eg:

$num=1345,25;
TNX

my $num=120.269;
print $num, "\n";
$num =~ s/\./,/;
print $num, "\n";
regards,
Reinhard
 
J

Josef Moellers

PERO said:
How to change decimal point ',' to decimal comma ','
Huh?

eg:

$num=1345,25;

You could try using a text editor.
Ages ago, a suitable suggestion would be to post it on Usenet ...

What exactly are you trying to accomplish, what code have you written so
far and where does your code not meet your expectations?
 
M

Mirco Wahab

PERO said:
How to change decimal point ',' to decimal comma ','

eg:

$num=1345,25;
TNX

Wild guess:

...

use POSIX;

my $num = "1345,25"; # ==> '1345,25' as "numeric literal"

setlocale(LC_ALL, "de_DE"); # "de" has comma as decimal point
my $m = strtod( $num ); # $m ==> '1345.25' now converted!
printf "%.3f\n", $m+$m;

setlocale(LC_ALL, "C");
printf "%.3f\n", $m+$m;

...


prints here (5.8.8 on Linux):

2690,500
2690.500

which is what one would expect.

Regards

Mirco
 
B

Brian McCauley

How to change decimal point ',' to decimal comma ','

eg:

$num=1345,25;
TNX

There is AFAIK no way to get Perl to recognise a decimal comma in
numeric literals in Perl source code.

If you are talking about _data_ not _code_ then the locale-based
solutions or simple string manipulations will do the trick. For string
manipulations I'd use tr/./,/ rather than s/\./,/.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top