PDF::Reuse and number rounding/precision

Discussion in 'Perl Misc' started by Justin C, Dec 5, 2008.

  1. Justin C

    Justin C Guest

    I'm using PDF::Reuse to produce output in a PDF template. The problems
    I'm having are:
    * I need to print currency values with two decimal places regardless of
    whether the number has anything after the decimal point.
    * I need to print values for weight with up to four digits after the
    decimal point (if there are digits).

    What PDF::Reuse is doing is only printing integars where there is
    nothing after the decimal for my currency output, and printing fourteen
    digits after the decimal in places where I need rounding.

    I'm using the following to produce output:

    prText($x, $y, $string)

    $x and $y are horizontal and vertical coordinates to the location to
    start printing $string.

    Normally I'd use printf to force rounding or decimals where none are
    displayed, but I can't see a way of using that here. The PDF::Reuse
    documentation does't provide help on this problem either.

    Does anyone have any suggestions on how to force, for example '34' to be
    '34.00', and 7.[lots of digits] to truncate at 4 digits after the
    decimal?

    All pointers to documentation or suggestions gratefully received.

    Justin.

    --
    Justin C, by the sea.
     
    Justin C, Dec 5, 2008
    #1
    1. Advertising

  2. Justin C

    Ted Byers Guest

    On Dec 5, 10:27 am, bugbear <bugbear@trim_papermule.co.uk_trim> wrote:
    > Justin C wrote:
    > > I'm using PDF::Reuse to produce output in a PDF template. The problems
    > > I'm having are:
    > > * I need to print currency values with two decimal places regardless of
    > >   whether the number has anything after the decimal point.
    > > * I need to print values for weight with up to four digits after the
    > >   decimal point (if there are digits).

    >
    > > What PDF::Reuse is doing is only printing integars where there is
    > > nothing after the decimal for my currency output, and printing fourteen
    > > digits after the decimal in places where I need rounding.

    >
    > > I'm using the following to produce output:

    >
    > >     prText($x, $y, $string)

    >
    > > $x and $y are horizontal and vertical coordinates to the location to
    > > start printing $string.

    >
    > > Normally I'd use printf to force rounding or decimals where none are
    > > displayed, but I can't see a way of using that here. The PDF::Reuse
    > > documentation does't provide help on this problem either.

    >
    > > Does anyone have any suggestions on how to force, for example '34' to be
    > > '34.00', and 7.[lots of digits] to truncate at 4 digits after the
    > > decimal?

    >
    > wouldn't
    >
    >     prText($x, $y, sprintf("%.2f", $string));
    >
    > be helpful?
    >
    > (untested, worth what you paid)
    >
    >    BugBear- Hide quoted text -
    >
    > - Show quoted text -


    While we don't use PDF::Reuse, we do routinely produce PDF reports,
    and your call to sprintf is precisely what we used (actually within
    our own function to add currency symbols and commas). Now we prefer
    the Data::Currency package, but be warned you need to pay attention to
    locale issues if you need to display, say, the Euro symbol (it is
    easier to just use the character code for the currency, such as USD
    for US dollars). This package makes it easier to get handling of
    symbols and delimiters right for different currencies.

    Cheers,

    Ted
     
    Ted Byers, Dec 5, 2008
    #2
    1. Advertising

  3. Justin C

    J. Gleixner Guest

    Justin C wrote:
    > I'm using PDF::Reuse to produce output in a PDF template. The problems
    > I'm having are:
    > * I need to print currency values with two decimal places regardless of
    > whether the number has anything after the decimal point.
    > * I need to print values for weight with up to four digits after the
    > decimal point (if there are digits).
    >
    > What PDF::Reuse is doing is only printing integars where there is
    > nothing after the decimal for my currency output, and printing fourteen
    > digits after the decimal in places where I need rounding.
    >
    > I'm using the following to produce output:
    >
    > prText($x, $y, $string)
    >
    > $x and $y are horizontal and vertical coordinates to the location to
    > start printing $string.
    >
    > Normally I'd use printf to force rounding or decimals where none are
    > displayed, but I can't see a way of using that here. The PDF::Reuse
    > documentation does't provide help on this problem either.


    Complete guess..

    prText($x, $y, sprintf( "%.2f", $string) );

    >
    > Does anyone have any suggestions on how to force, for example '34' to be
    > '34.00', and 7.[lots of digits] to truncate at 4 digits after the
    > decimal?


    sprintf( "%.4f", $val )

    >
    > All pointers to documentation or suggestions gratefully received.


    perldoc -f sprintf
     
    J. Gleixner, Dec 5, 2008
    #3
  4. Justin C

    Ted Byers Guest

    On Dec 5, 11:01 am, Ted Byers <> wrote:
    > On Dec 5, 10:27 am, bugbear <bugbear@trim_papermule.co.uk_trim> wrote:
    >
    >
    >
    >
    >
    > > Justin C wrote:
    > > > I'm using PDF::Reuse to produce output in a PDF template. The problems
    > > > I'm having are:
    > > > * I need to print currency values with two decimal places regardless of
    > > >   whether the number has anything after the decimal point.
    > > > * I need to print values for weight with up to four digits after the
    > > >   decimal point (if there are digits).

    >
    > > > What PDF::Reuse is doing is only printing integars where there is
    > > > nothing after the decimal for my currency output, and printing fourteen
    > > > digits after the decimal in places where I need rounding.

    >
    > > > I'm using the following to produce output:

    >
    > > >     prText($x, $y, $string)

    >
    > > > $x and $y are horizontal and vertical coordinates to the location to
    > > > start printing $string.

    >
    > > > Normally I'd use printf to force rounding or decimals where none are
    > > > displayed, but I can't see a way of using that here. The PDF::Reuse
    > > > documentation does't provide help on this problem either.

    >
    > > > Does anyone have any suggestions on how to force, for example '34' tobe
    > > > '34.00', and 7.[lots of digits] to truncate at 4 digits after the
    > > > decimal?

    >
    > > wouldn't

    >
    > >     prText($x, $y, sprintf("%.2f", $string));

    >
    > > be helpful?

    >
    > > (untested, worth what you paid)

    >
    > >    BugBear- Hide quoted text -

    >
    > > - Show quoted text -

    >
    > While we don't use PDF::Reuse, we do routinely produce PDF reports,
    > and your call to sprintf is precisely what we used (actually within
    > our own function to add currency symbols and commas).  Now we prefer
    > the Data::Currency package, but be warned you need to pay attention to
    > locale issues if you need to display, say, the Euro symbol (it is
    > easier to just use the character code for the currency, such as USD
    > for US dollars).  This package makes it easier to get handling of
    > symbols and delimiters right for different currencies.
    >
    > Cheers,
    >
    > Ted- Hide quoted text -
    >
    > - Show quoted text -


    OOPS! I forgot to mention that fine package:

    Locale::Currency::Format

    Worth checking out.

    Ted
     
    Ted Byers, Dec 5, 2008
    #4
  5. Justin C

    Justin C Guest

    On 2008-12-05, J. Gleixner <> wrote:
    > Justin C wrote:
    >> I'm using PDF::Reuse to produce output in a PDF template. The problems
    >> I'm having are:
    >> * I need to print currency values with two decimal places regardless of
    >> whether the number has anything after the decimal point.
    >> * I need to print values for weight with up to four digits after the
    >> decimal point (if there are digits).
    >>
    >> What PDF::Reuse is doing is only printing integars where there is
    >> nothing after the decimal for my currency output, and printing fourteen
    >> digits after the decimal in places where I need rounding.
    >>
    >> I'm using the following to produce output:
    >>
    >> prText($x, $y, $string)
    >>
    >> $x and $y are horizontal and vertical coordinates to the location to
    >> start printing $string.
    >>
    >> Normally I'd use printf to force rounding or decimals where none are
    >> displayed, but I can't see a way of using that here. The PDF::Reuse
    >> documentation does't provide help on this problem either.

    >
    > Complete guess..
    >
    > prText($x, $y, sprintf( "%.2f", $string) );


    Thanks J, and Bug Bear. Also thanks to Ted, though I shan't be needing
    the currency symbols at this time.

    The thought of using sprintf.... hmmmm, actually, I don't know
    sprintf... I read that as printf, but put tried sprintf. I've now read
    the sprintf doc, I'm sure I could have used this many times before! It
    surprises me how far one can get without knowledge of some commands...

    Excellent stuff, problem solved.

    Justin.

    --
    Justin C, by the sea.
     
    Justin C, Dec 8, 2008
    #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. tshad
    Replies:
    5
    Views:
    561
    Steve C. Orr [MVP, MCSD]
    May 17, 2005
  2. Hylander

    To reuse or not to reuse....

    Hylander, Feb 26, 2004, in forum: Java
    Replies:
    0
    Views:
    441
    Hylander
    Feb 26, 2004
  3. code reuse and design reuse

    , Feb 7, 2006, in forum: C Programming
    Replies:
    16
    Views:
    1,066
    Malcolm
    Feb 12, 2006
  4. jacob navia

    To reuse or not to reuse

    jacob navia, Nov 5, 2006, in forum: C Programming
    Replies:
    19
    Views:
    568
    Dave Thompson
    Dec 18, 2006
  5. Ricardo Pog
    Replies:
    1
    Views:
    491
    Austin Ziegler
    Mar 26, 2008
Loading...

Share This Page