Pregunta

I'm currently having to interface with a legacy Fortran program by creating an ascii output file that the program needs to read in, perform some calculations, and then I have to parse the binary output. For the most part, I have this working, but I'm running into a slight snag.

The problem I'm running into is with Fortran descriptors. Is there an easy way I can get a descriptor like E9.3 into a DecimalFormat object in Java?

The DecimalFormat I have works only for numbers less than 1. For numbers < 1 such as 0.123, they are written out with the sign on the exponential, as 1.230E-01. However, for numbers greater than 1 (12.345), they are written out as 1.234E01 rather than 1.234E+01. From what I'm being told, the program is expecting that '+' sign as part of the exponential.

The DecimalFormat string I'm currently using is "#0.000E00", and I'm currently pulling some string manipulation to insert the '+' in there (after the E but before the digits) if necessary. There has to be a better way to do this. Any assistance would be appreciated.

¿Fue útil?

Solución

Are you really sure you need this? Maybe it's only a compiler extension, but it's worth checking if the Fortran program actually accepts your form 1.234E01. My both compilers (gfortran and Solaris Studio) do accept them with E9.3 edit descriptor.

Otros consejos

If you have access to the source of the fortran program, you could change it to read the input file using the * descriptor, e.g. if you read in an array data of n values, e.g. change this:

read(iUnit,'(10E9.3)') data(10)

to something like:

read(iUnit, *) data(10)

or to write the file unformatted and then read in the binary data.

If you need the whole string to be 9 characters, it means there'll be three characters left for the decimal part. You can express this with printf style formatting like this:

groovy:000> String.format("%.3E", 0.000123456789)         
===> 1.235E-04
groovy:000> String.format("%.3E", 1234.56789)         
===> 1.235E+03

If you need it to be nine characters in total even for negative numbers, with the leading '-' counting against the character count, you'll have to us an if to use a different format for negative numbers.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top