Reputation: 417
This is probably a very basic question but I can't seem to find the answer on google or through searching though this forum (most likely due to poor phraseology of the problem).
I am looking for an intrinsic Fortran function to give me the number of positions used by an integer. What I mean by this is, if an integer is 12543 then the number of positions used is 5 (it will always be positive). The reason I want this is so that when using a WRITE statement with formatted output I can specify the "w" in "Iw" and use the minimum spacing possible.
More specifically:
WRITE(*,"(/ A,Iw,1X,A,Iw)") "All particles within radius ",radius, "of particle index number", p_o_i
Where radius and p_o_i are specified by the user earlier on.
I can write something to achieve this easily enough but I thought that there must be an intrinsic function which can achieve this.
I have been going through the functions here but without much luck. Thanks for the help.
Upvotes: 4
Views: 3538
Reputation: 417
Thanks for the responses! I admit that the question was misleading in that it specifically requested an intrinsic Fortran function to give the number of positions used by an integer when this was not strictly required in the first place. Furthermore, the general consensus seems to be that this does not exist.
As advised by francescalus and High Performance Mark, in order to ensure that the minimum spacing possible is used for an integer, the "I0" edit description should be used. So for the previous example this would be:
WRITE(*,"(/ 1X, A, I0, A, I0)") "All particles within radius ",radius, " of particle index number ", p_o_i
Should you actually need to find the number of "characters" in an integer (for some other reason) the best proposed response seems to be the use of the log10
function, as advised by steabert.
Upvotes: 2