Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28994 Discussions

How to determine character versus numeric data type

michael_green
Beginner
758 Views
Hi All,

I need to determine whether a string represents numeric data or character data. My method for this has always been something like this:

real*4 num

read(FieldValues(i,j),*,iostat=ios)num

If ios has value 0 it's numeric, otherwise it's a character string.

Unfortunately the data set I am working with at present often contains a single "/" as the data item, and this is read successfully as a number. So I have two questions: why is "/" read as a number? and what is the best way to determine whether a string represents a number or just characters?

With many thanks in advance

Mike
0 Kudos
3 Replies
DavidWhite
Valued Contributor II
758 Views
Mike,

What happens if you put a format specifier instead if * in the read statement?

Additionally, you can check for the presence of only 0...9, ., +, -, E, D any other characters would definitely mean it is a character string.

David
0 Kudos
michael_green
Beginner
758 Views
Hi David,

I replaced the * with an unqualified "F" - and it works! So easy, why didn't I think of it?

Many thanks
Mike
0 Kudos
Steven_L_Intel1
Employee
758 Views
This is a classic trap Fortran programmers fall into. List-directed formatting is usually much more liberal in what it will accept than is explicit formatting. Note that just "F" will provide a default width and number of fraction digits. If the user does not enter a decimal point, you may get unexpected results. I suggest instead 'BN,F12.0' where the 12 is whatever the width of your string is. BN makes sure that blanks are ignored (they would be by default, unless you used certain compatibility switches such as /vms.)
0 Kudos
Reply