PDF::Reuse and number rounding/precision

J

Justin C

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

Ted Byers

Justin said:
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
 
J

J. Gleixner

Justin said:
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
 
T

Ted Byers

Justin said:
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?

    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
 
J

Justin C

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top