Velocity Reviews > Perl > Number padding... (trailing zeros'.)

ThePotPlants
Guest
Posts: n/a

 05-23-2004
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

Dave Cross
Guest
Posts: n/a

 05-23-2004
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...

Jay Tilton
Guest
Posts: n/a

 05-23-2004
"ThePotPlants" <(E-Mail Removed)> 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

Jürgen Exner
Guest
Posts: n/a

 05-23-2004
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

ThePotPlants
Guest
Posts: n/a

 05-23-2004

"Dave Cross" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> 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

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Frisbee® MCSE 37 09-26-2005 04:06 PM mayur ASP .Net 2 07-02-2004 10:35 AM hari VHDL 6 05-02-2004 04:10 PM taras ASP .Net 1 04-17-2004 04:51 AM makok VHDL 1 02-23-2004 06:04 PM