Number padding... (trailing zeros'.)

T

ThePotPlants

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
 
D

Dave Cross

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

Jay Tilton

: 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

Jürgen Exner

ThePotPlants said:
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
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top