Number padding... (trailing zeros'.)

Discussion in 'Perl Misc' started by ThePotPlants, May 23, 2004.

  1. ThePotPlants

    ThePotPlants Guest

    Hi

    Is there a nice way of formatting a decimal to have a fixed number of
    trailing zero's?
    I am decimalising some data to feed into another application. The input
    requires 6 decimal places.
    Using sprintf I can apply various formats, like leading zero's but can't
    figure out how to add trailers...

    For most of my data it's not a problem, but I have a couple of oddities that
    divide neatly leading me with 4 decimal places.
    One example I have. After conversion I get: 0.1029 but I want 0.102900.

    At the moment I am multiplying my variable by 100000 and doing a substr on
    the leading 6 characters, but there must be a nicer way than this...

    Any help would be much appreciated.

    Pete
     
    ThePotPlants, May 23, 2004
    #1
    1. Advertising

  2. ThePotPlants

    Dave Cross Guest

    On Sun, 23 May 2004 19:24:11 +1200, ThePotPlants wrote:

    > Hi
    >
    > Is there a nice way of formatting a decimal to have a fixed number of
    > trailing zero's?
    > I am decimalising some data to feed into another application. The input
    > requires 6 decimal places.
    > Using sprintf I can apply various formats, like leading zero's but can't
    > figure out how to add trailers...


    $ perl -le 'printf "%.6f", 0.1029'
    0.102900

    Dave...
     
    Dave Cross, May 23, 2004
    #2
    1. Advertising

  3. ThePotPlants

    Jay Tilton Guest

    "ThePotPlants" <> wrote:

    : Is there a nice way of formatting a decimal to have a fixed number of
    : trailing zero's?

    Yes.

    : One example I have. After conversion I get: 0.1029 but I want 0.102900.

    printf '%.6f', 0.1029
     
    Jay Tilton, May 23, 2004
    #3
  4. ThePotPlants wrote:
    > Is there a nice way of formatting a decimal to have a fixed number of
    > trailing zero's?
    > I am decimalising some data to feed into another application. The
    > input requires 6 decimal places.
    > Using sprintf I can apply various formats, like leading zero's but
    > can't figure out how to add trailers...


    Hmmm, strange.
    Doesn't the second example on the sprintf perldoc page work for?

    jue
     
    Jürgen Exner, May 23, 2004
    #4
  5. ThePotPlants

    ThePotPlants Guest

    "Dave Cross" <> wrote in message
    news:p...
    > On Sun, 23 May 2004 19:24:11 +1200, ThePotPlants wrote:
    >
    > > Hi
    > >
    > > Is there a nice way of formatting a decimal to have a fixed number of
    > > trailing zero's?

    > $ perl -le 'printf "%.6f", 0.1029'
    > 0.102900
    >
    > Dave...


    Thanks guys :)
     
    ThePotPlants, May 23, 2004
    #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. Michael

    Printing double with no trailing zeros

    Michael, Nov 14, 2003, in forum: C Programming
    Replies:
    3
    Views:
    4,712
    Martin Ambuhl
    Nov 14, 2003
  2. Replies:
    5
    Views:
    326
    Peter Otten
    Aug 22, 2011
  3. BARBARA FLOYD

    ASP IN EXCEL, trailing zeros truncated

    BARBARA FLOYD, Nov 11, 2003, in forum: ASP General
    Replies:
    1
    Views:
    174
    Ray at
    Nov 11, 2003
  4. Replies:
    4
    Views:
    128
  5. Replies:
    4
    Views:
    107
    PyNoob
    Jun 27, 2013
Loading...

Share This Page