0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Fortran 数値から文字列、文字列から数値への変換

Last updated at Posted at 2024-06-13

はじめに

Fortranでは標準ライブラリで数値から文字列、文字列から数値への直接変換するものが用意されておらず、何かを組み合わせたり別の仕組みを利用して行う必要がある。

古い書き方

program main
    use, intrinsic :: iso_fortran_env, only: real64, int32
    implicit none
    character(:), allocatable :: str
    character(:), allocatable :: str2
    character(:), allocatable :: str3
    integer(int32) :: i
    real(real64) :: pi

    ! integer->文字列
    allocate (character(len=30) :: str)
    write (str, *) 1234567890
    print *, "|", str, "|"
    str = trim(adjustl(str))
    print *, "|", str, "|"

    ! real->文字列(書式を省略する場合)
    allocate (character(len=30) :: str3)
    write (str3, *) 3.14159265_real64
    print *, "|", str3, "|"
    str3 = trim(adjustl(str3))
    print *, "|", str3, "|"

    ! real->文字列(小数点以下の桁数を指定する場合)
    allocate (character(len=30) :: str2)
    write (str2, '(F0.8)') 3.14159265_real64 !F0で左詰めになる。
    print *, "|", str2, "|"
    str2 = trim(str2)
    print *, "|", str2, "|"

    ! 文字列->integer
    str = "1234567890"
    read(str, *) i
    print *, i

    ! 文字列->real
    str = '3.14159265'
    read(str, *) pi
    print *, pi

end program main
output
 |  1234567890                  |
 |1234567890|
 |   3.1415926500000002         |
 |3.1415926500000002|
 |3.14159265                    |
 |3.14159265|
  1234567890
   3.1415926500000002    

数値から文字列に変換する際はallocatableな文字列を使い。あらかじめ必要な文字数を考え多めに文字数を割り当ててwrite文で入力する。それからtrim関数,adjustl関数を使って前後の空白を削り取る。
(F0.8)といったrealの書式は小数点以下8桁かつ左詰めになるため、指定したい桁数だけを入力すればよいため便利である。
文字列から数値に関してはread文を使用すればよい。

文字列から数値の場合は簡単だが、数値から文字列へ変換はallocateやwriteが必要でややこしい。次のstdlibを利用した書き方が簡単である。

stdlibを利用した書き方

program main
    use, intrinsic :: iso_fortran_env, only: real64, int32
    use stdlib_strings, only: to_string
    use stdlib_str2num, only: to_num
    implicit none
    character(:), allocatable :: str
    integer(int32) :: num, digit
    real(real64) :: val

    ! integer->文字列(書式あり)
    str = to_string(123, '(I30.5)')
    print *, "|", str, "|"

    ! integer->文字列(書式なし)
    str = to_string(1234567890)
    print *, "|", str, "|"

    ! real->文字列(書式あり)
    digit = 8
    str = to_string(3.14159265_real64, '(F0.'//to_string(digit)//')') !小数点以下8桁で表示
    print *, "|", str, "|"

    ! real->文字列(書式なし)
    str = to_string(3.14159265_real64)
    print *, "|", str, "|"

    ! 文字列->integer
    str = "12345"
    num = to_num(str, num)
    print *, num

    ! 文字列->real
    str = "2.34"
    val = to_num(str, val)
    print *, val

end program main
output
 |                         00123|
 |1234567890|
 |3.14159265|
 |3.1415926500000002|
       12345
   2.3399999999999999  

to_string関数は
string = to_string (value [, format])という形式で、数値と必要なら書式を入力する。
to_stringでは(F20.8)(I30.5)などの先頭に空白がありえそうなものを除き、基本的に空白を詰めた文字列になる。
integerなら書式なし、realなら(F0.8)という書式で出力したほうが一番いいと思われる。

to_num関数は
number = to_num (string, mold)という形式で数値が入った文字列とmoldと呼ばれる変数を入れる。
to_numは総称名で、moldの型に合わせて、例えばinteger型ならinteger型を返す関数を使ってinteger型を出力する。
基本的には、上記の例のように戻り値の変数を入力すればよい。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?