Fortran: String to Real Conversion

In summary, the conversation discusses the difficulty of converting a string to a Real data type in Fortran. The person has tried using Write() but it did not work. They are looking for a solution and provide a quick example of how to do it using an internal read. They also suggest searching online for more information and provide some helpful links.
  • #1
DizzyDoo
13
0
This has been bugging my (and my program) for a few days now. I'm searching for a way to convert a string (or more properly; a collect of characters) to a Real data type. I'm new to Fortran, but as I understand it there seems to be no intrinsic function to make this easy.

I have tried various solutions to this problem, just as using Write(*,*) to write the string to the real, but the real seems to stubbornly stay 0.

The reason I require this is that I'm taking data straight from the command line which is parsed in string format. Other information that may be useful is that my compiler of choice is the G77 compiler.

Solution to this problem would be must appreciated,
Matt
 
Technology news on Phys.org
  • #2
Here's a quick F77 example that demostrates how you can do it, an internal read (from a character string).
Code:
      REAL*8 A,B
      CHARACTER*80 LINE
      READ(5,999)LINE
      READ(LINE,998)A,B
      WRITE(6,998)A,B
      STOP
999   FORMAT(A80)
998   FORMAT(F10.2,F10.2)
      END
If you need more information, google "fortran internal read", or try the following links:
http://en.wikibooks.org/wiki/Fortran_FAQ
http://www-mipl.jpl.nasa.gov/portguide/subsection3.9.5.html
http://www.megasolutions.net/fortran/internal-read-of-character-array_F-ifort-differences-77847.aspx
 
Last edited by a moderator:
  • #3


Hello Matt,

I understand your frustration with trying to convert a string to a Real data type in Fortran. While there is no intrinsic function for this, there are a few ways you can approach this problem. One option is to use the READ statement, which allows you to read data from a string rather than from a file. Here is an example of how you could use it:

READ(string, *) real_variable

This will read the string into the real variable, converting it to the appropriate data type. Another option is to use the intrinsic function REAL, which can convert a character string to a real value. Here is how you could use it:

real_variable = REAL(string)

I suggest checking the documentation for your specific compiler to see if there are any specific functions or methods for converting strings to real values. Additionally, make sure the string you are trying to convert is in the correct format for a real value.

I hope this helps solve your problem and allows you to continue with your program. Best of luck!
 

Related to Fortran: String to Real Conversion

1. How do I convert a string to a real number in Fortran?

Fortran provides the REAL() function which can be used to convert a string to a real number. This function takes the string as an argument and returns the corresponding real number.

2. Can I convert a string with scientific notation to a real number in Fortran?

Yes, Fortran's REAL() function can handle strings with scientific notation. However, the string must be in the correct format, with the coefficient and exponent separated by the letter "E", for example, "1.23E4" for 12300.

3. What happens if the string cannot be converted to a real number in Fortran?

If the string cannot be converted to a real number, the REAL() function will return an error and the program will terminate. It is important to ensure that the string is in a valid format for conversion before using the function.

4. Are there any other methods for converting a string to a real number in Fortran?

Yes, there are other methods for converting a string to a real number in Fortran, such as the READ() statement and the VAL() function. However, the REAL() function is the most commonly used method.

5. Can I convert a string to a double precision real number in Fortran?

Yes, Fortran provides the DBLE() function which can be used to convert a string to a double precision real number. This function takes the string as an argument and returns the corresponding double precision real number.

Similar threads

  • Programming and Computer Science
Replies
9
Views
3K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
4
Views
15K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
9
Views
1K
Back
Top