For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > May 2004 > Number padding... (trailing zeros'.)









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Number padding... (trailing zeros'.)
ThePotPlants

2004-05-23, 4:31 am

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


Jay Tilton

2004-05-23, 4:31 am

"ThePotPlants" <thepotplants@yahoo.com> 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

Dave Cross

2004-05-23, 4:31 am

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

Jürgen Exner

2004-05-23, 4:31 am

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

2004-05-23, 7:31 pm


"Dave Cross" <dave@dave.org.uk> wrote in message
news:pan.2004.05.23.07.32.03.598268@dave.org.uk...
> On Sun, 23 May 2004 19:24:11 +1200, ThePotPlants wrote:
>
> $ perl -le 'printf "%.6f", 0.1029'
> 0.102900
>
> Dave...


Thanks guys :)


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com