How to switch floating decimal number char from "." to "," ?

P

Pete Sammet

As I found out by default Perl produces floating point number output as

123456.78

where ".78" is the fraction part of the number.

However in Europe another format is used:

123456,78

How exactly can I switch from the first to the second format?

I read a solution with

$myvar ~= tr/./,/;

but I don't want such a "afterwork" transformation.

The output should AUTOMATICALLY contain "," even during the calculation:

$num = 5/4;
print $num;

should show 1,25

Pete
 
M

Martijn Lievaart

This is a bad plan. Locales (specifically, the 'locale' pragma) and
Unicode don't play nicely together in Perl, and if you're processing
international text you will probably end up with Unicode strings. A

Can you expand on this? What exactly goes wrong (or is unexpected)?

M4
 
M

Martijn Lievaart

(snip)

Confused yet? :)

That's just plain buggy I would say, or is there some logic I don't see?

Besides, your examples did not work for me completely, to get the same
regex matching I had to set LANG as well.

M4
 
M

Martijn Lievaart

No, it's just plain buggy. The bugs have been there since 5.8.0, they
are well known, and the only reason they haven't been fixed yet is
because it's extremely difficult (both to work out what the behaviour
*should* be, and to write the actual code). The problem *is* currently
being worked on (mostly by Karl Williamson), but don't hold your breath.

Thx for the info.

/me writes down after "never use threads in Perl", "never use locales in
Perl".

I get the problem, but it does suck.
You probably had LANG set in your environment already, which I don't.
IIRC LANG overrides LC_ALL.

Ah, that explains it.

M4
 
P

Peter J. Holzer

That's just plain buggy I would say, or is there some logic I don't see?

The logic is that if you use locale then byte strings are supposed to be
encoded according to the current locale. This affects only regexps and
string comparisons according to perldoc locale. It could be argued that
it should also affect implicit upgrading to character strings.
Besides, your examples did not work for me completely, to get the same
regex matching I had to set LANG as well.

That looks like a bug. LC_ALL is supposed to override LANG.

hp
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top