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

Discussion in 'Perl Misc' started by Pete Sammet, Jun 4, 2010.

  1. Pete Sammet

    Pete Sammet Guest

    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
    Pete Sammet, Jun 4, 2010
    #1
    1. Advertising

  2. On 04/06/10 11:15, Pete Sammet wrote:

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


    > However in Europe another format is used:


    > 123456,78


    First hit of google for "perl locale" looked relevant.

    Rgds

    Denis McMahon
    Denis McMahon, Jun 4, 2010
    #2
    1. Advertising

  3. On Sat, 05 Jun 2010 21:33:20 +0100, Ben Morrow wrote:

    > 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
    Martijn Lievaart, Jun 7, 2010
    #3
  4. On Mon, 07 Jun 2010 17:21:07 +0100, Ben Morrow wrote:

    > Quoth Martijn Lievaart <>:
    >> On Sat, 05 Jun 2010 21:33:20 +0100, Ben Morrow wrote:
    >>
    >> > 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)?

    >

    (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
    Martijn Lievaart, Jun 7, 2010
    #4
  5. Re: locale and Unicode [Was: How to switch floating decimal...]

    On Mon, 07 Jun 2010 18:27:08 +0100, Ben Morrow wrote:

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

    >
    > 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.

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

    >
    > You probably had LANG set in your environment already, which I don't.
    > IIRC LANG overrides LC_ALL.
    >


    Ah, that explains it.

    M4
    Martijn Lievaart, Jun 7, 2010
    #5
  6. On 2010-06-07 17:00, Martijn Lievaart <> wrote:
    > On Mon, 07 Jun 2010 17:21:07 +0100, Ben Morrow wrote:
    >> Quoth Martijn Lievaart <>:
    >>> On Sat, 05 Jun 2010 21:33:20 +0100, Ben Morrow wrote:
    >>>
    >>> > 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)?

    >>

    > (snip)
    >>
    >> Confused yet? :)

    >
    > 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
    Peter J. Holzer, Jun 7, 2010
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. makok
    Replies:
    1
    Views:
    6,752
    Anders Hellerup Madsen
    Feb 23, 2004
  2. muss
    Replies:
    13
    Views:
    709
    Vladimir S. Oka
    Mar 27, 2006
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,015
    Ian Collins
    May 9, 2006
  4. Alejandro
    Replies:
    2
    Views:
    403
    Dan Lenski
    Aug 18, 2008
  5. Chris Rebert
    Replies:
    2
    Views:
    277
    Davish Bhardwaj
    Jan 11, 2011
Loading...

Share This Page