Digit Separator

Does Fortran have a digit separator for integers? Seems like a good feature to have if it is not already there.
https://en.cppreference.com/w/cpp/language/integer_literal

Not as far as I know.
But lol, you could use F77 fixed format, where space is insignificant:

c This is a fixed form file (.f)
      program test
      i = 1 000 000
      print *, i
      end program

One can also use spaces as digits separator in input data, with explicit format and blanks interpretation set to ‘null’ (default) or forced by bn control descriptor

program blanks
  read(*,'(bn,i20)') i      !   1 234 567
  print *, i                !     1234567
end program blanks
2 Likes

@fortran4r , if your questions pertains to integer literals, as in say what the Fortran standard terms int-literal-constant, the conforming digit-string per the current standard allows no spaces or any other separators.

The BNF form being: digit[digit]...

I like they way Rust achieves this:

// Use underscores to improve readability!
println!("One million is written as {}", 1_000_000u32);

A Fortran compiler would try to interpret this as a 1 of the type 000_000u32 which obviously cannot exist.

But I think this could easily be added to the Fortran standard.
Maybe like this:

print *, "One million: ",  1_000_000_int32

I don’t think this would break anything because types have to begin with non-digits.

But for reals it might get a bit trickier:

print *, 3_141.59_e-3

This could be 3141.59 of type e minus 3

However, trailing underscores should simply be forbidden.

As far as I know it will break existing codes. If an integer kind is equal, for example, to 4, you can write: 1234_4.
Meaning it is an integer constant with an integer kind of 4.

1 Like