Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TCL/TK format force width

Status
Not open for further replies.

Arnoshka

Technical User
Jul 10, 2009
17
DE
Hello,
I'm having big trouble finding a formating that fits my needs.

I have to create lists with numbers of maximum 8 characters.
Currently I'm using the "g" formatt,
Code:
return [format "%8g%8g%8g" $coordx $coordy $coordz]
but my variables "coordx", "coordy" und "coordz" are often close to zero null and in TCL this might give me a number like: 58.88178e-016.
Is there a possibility to force a number to a specific width??

Code:
0.123456 ->       0.123456
-0.123456 ->      -0.12345
58.88178e-016 ->  58.8e-16
-58.88178e-016 -> -58.e-16
...

thanks in advance
Arno
 
You posted Tcl question in the Fortran Forum.
It would be better to post it in this Tcl/Tk forum:
Are you using any Tcl extension with Fortran?

Because the Tcl format command(see is similar to C sprintf,
I think that the number of digits in the scientific notation produced by %e , %E , %g, %G is system-dependent
If you need maximal 8 characters for number near to zero, than 1 character would be for eventually sign, 1 character for decimal point and 5 characters for decimal places.
Maybe you can use %.5f in format.

This in Tcl
Code:
set coordx 0.123456 
set coordy -0.123456
set coordz 58.88178e-016
set coordt -58.88178e-016

puts [format "%.5f %.5f %.5f %.5f" $coordx $coordy $coordz $coordt]
outputs these numbers
Code:
0.12346 -0.12346 0.00000 -0.00000
 
Oh I didn't notice I wrote in the FORTRAN forum.

And thank you for the advice. but the format still does not force the number to the field width.

Code:
set coordx 4321.123456
puts [format "%.5f" $coordx]
returns
Code:
4321.123456
instead of
Code:
4321.123
 
Ok I'll post the question on the TCL/TK Forum.
 
Ok, I thought your numbers' absolute values are between 0..1, e.g. 0.123456
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top