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

Discussion in 'Perl Misc' started by PERO, Apr 13, 2007.

  1. PERO

    PERO Guest

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

    eg:

    $num=1345,25;
    TNX
     
    PERO, Apr 13, 2007
    #1
    1. Advertisements

  2. PERO wrote:
    > 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

    --
    PM Mails an rpirpag <at> gmx dot at
     
    Reinhard Pagitsch, Apr 13, 2007
    #2
    1. Advertisements

  3. PERO wrote:
    > 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?

    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, Apr 13, 2007
    #3
  4. PERO

    Mirco Wahab Guest

    PERO wrote:
    > 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
     
    Mirco Wahab, Apr 13, 2007
    #4
  5. On Apr 13, 1:09 pm, "PERO" <> wrote:
    > 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/\./,/.
     
    Brian McCauley, Apr 13, 2007
    #5
    1. Advertisements

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. Rasool
    Replies:
    3
    Views:
    569
    mikeb
    Mar 3, 2004
  2. ibiza
    Replies:
    6
    Views:
    10,649
    ibiza
    Jan 20, 2006
  3. JohnC
    Replies:
    7
    Views:
    1,920
    Peter Flynn
    Jun 7, 2008
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,495
    Saraswati lakki
    Jan 6, 2012
  5. Jeff Beardsley

    A possible change to decimal.Decimal?

    Jeff Beardsley, Mar 2, 2012, in forum: Python
    Replies:
    5
    Views:
    282
    Ethan Furman
    Mar 4, 2012
Loading...

Share This Page