number formatting..

Discussion in 'Perl Misc' started by Jaan Kronberg, Nov 27, 2003.

  1. Hello there,


    my $somenumber = 1000000;

    I'd like to format this $somenumber to look like 1.000.000,00
    Can it be done with sprintf? Other ways?

    Thx,
    jk
     
    Jaan Kronberg, Nov 27, 2003
    #1
    1. Advertising

  2. "Jaan Kronberg" <> wrote in message
    news:3fc5f7fb$0$155$...
    > my $somenumber = 1000000;
    >
    > I'd like to format this $somenumber to look like 1.000.000,00
    > Can it be done with sprintf? Other ways?


    perldoc -q "numbers with commas"

    should get you on your way.

    Paul
     
    Paul Boardman, Nov 27, 2003
    #2
    1. Advertising

  3. Jaan Kronberg

    Tore Aursand Guest

    On Thu, 27 Nov 2003 15:11:25 +0200, Jaan Kronberg wrote:
    > my $somenumber = 1000000;
    >
    > I'd like to format this $somenumber to look like 1.000.000,00
    > Can it be done with sprintf? Other ways?


    This is partly a FAQ;

    perldoc -q commas

    The decimal part is solved by using sprintf, though.


    --
    Tore Aursand <>
    "A teacher is never a giver of truth - he is a guide, a pointer to the
    truth that each student must find for himself. A good teacher is
    merely a catalyst." -- Bruce Lee
     
    Tore Aursand, Nov 27, 2003
    #3
  4. Jaan Kronberg wrote:
    >
    > my $somenumber = 1000000;
    >
    > I'd like to format this $somenumber to look like 1.000.000,00
    > Can it be done with sprintf? Other ways?


    This is a three step solution:

    sub numfmt {
    local $_ = shift;
    $_ = sprintf '%.2f', $_; # 1. Decimals
    tr/./,/; # 2. Comma as dec. point
    1 while s/^([-+]?\d+)(\d{3})/$1.$2/; # 3. Points added
    return $_;
    }
    print numfmt($somenumber);

    I suppose it's possible to have Perl use a comma as the decimal point
    by using the locale pragma, but I didn't succeed when testing it.

    As Paul and Tore mentioned, there is a Q/A in the FAQ that provides a
    couple of methods for adding commas to numbers. I used one of those
    methods, exchanging the comma for a point.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Nov 27, 2003
    #4
  5. Jaan Kronberg

    David Guest

    On Thu, 27 Nov 2003 15:11:25 +0200, "Jaan Kronberg"
    <> wrote:

    >Hello there,
    >
    >
    >my $somenumber = 1000000;
    >
    >I'd like to format this $somenumber to look like 1.000.000,00
    >Can it be done with sprintf? Other ways?
    >
    >Thx,
    >jk
    >


    If you want a quick way.

    use Number::Format;
    my $nu = new Number::Format(
    -thousands_sep => ',',
    -decimal_point => '.'
    );

    $nu->format_number($somenumber);
     
    David, Dec 1, 2003
    #5
    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. rguti

    Number formatting

    rguti, Jun 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    403
    Craig Deelsnyder
    Jun 10, 2004
  2. Luis Esteban Valencia

    Formatting decimal number

    Luis Esteban Valencia, Jan 12, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    534
    Philip Q [MVP]
    Jan 12, 2005
  3. probashi

    Number Formatting Question C#

    probashi, Feb 23, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    15,992
    Karl Seguin
    Feb 23, 2005
  4. thomson

    Number Formatting

    thomson, Aug 3, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    362
  5. Thor W Hammer

    Formatting a number without rounding

    Thor W Hammer, Nov 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    509
    Karl Seguin
    Nov 22, 2005
Loading...

Share This Page