# Replicating formatting from Fortran code output

**URL:** https://discourse.julialang.org/t/replicating-formatting-from-fortran-code-output/127036
**Category:** General Usage
**Tags:** fortran, formatting
**Created:** [March 17, 2025, 3:07am UTC](https://discourse.julialang.org/t/replicating-formatting-from-fortran-code-output/127036 "2025-03-17T03:07:07Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![trung](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trung/32/49217_2.png) [@trung](https://discourse.julialang.org/u/trung)
#### Post date: [March 17, 2025, 3:07am UTC](https://discourse.julialang.org/t/replicating-formatting-from-fortran-code-output/127036/1 "2025-03-17T03:07:07Z")

</div>

I am trying to replicate the formatting from a Fortran code output in Julia. Here is a MWE.

```julia
program formattest
    implicit none
    integer:: iyd
    real(4):: d(4),t

    iyd = 10000
    d = 9.999e-38
    t = 1.053e10

    open(77,file='outputtest.txt',status='replace')
    write(77,'(i7,4e13.4,f8.2)') &
        iyd,d(1:4),t
    close(77)
end program

```

The output that I obtain is

```julia
10000 0.9999E-37 0.9999E-37 0.9999E-37 0.9999E-37 294.10

```

Is it possible to replicate the formatting from the “0.9999E-37” in Julia?

---

<div class="post-metadata">

### Author: ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)
#### Post date: [March 17, 2025, 5:51am UTC](https://discourse.julialang.org/t/replicating-formatting-from-fortran-code-output/127036/2 "2025-03-17T05:51:15Z")

</div>

> [@trung](#):
>
> 0.9999E-37

It looks small for `real(4)`… Did you already play with `Printf` [Printf · The Julia Language](https://docs.julialang.org/en/v1/stdlib/Printf/) ?

---

<div class="post-metadata">

### Author: ![trung](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trung/32/49217_2.png) [@trung](https://discourse.julialang.org/u/trung)
#### Post date: [March 17, 2025, 11:55am UTC](https://discourse.julialang.org/t/replicating-formatting-from-fortran-code-output/127036/3 "2025-03-17T11:55:04Z")

</div>

The 9.999E-38 is something special put in the original code so there is no way around it and I already tried to use Printf but I don’t see any way to print it as `0.9999E-37` instead of `9.999E-38`

---

<div class="post-metadata">

### Author: ![jmkuhn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmkuhn/32/2090_2.png) [@jmkuhn](https://discourse.julialang.org/u/jmkuhn)
#### Post date: [March 17, 2025, 1:29pm UTC](https://discourse.julialang.org/t/replicating-formatting-from-fortran-code-output/127036/4 "2025-03-17T13:29:21Z")

</div>

I don’t know of a Julia package that will do this directly. There is a Python package that you could use with PyCall

```julia
using PyCall

fortranformat = pyimport("fortranformat")
format = fortranformat.FortranRecordWriter("(i7,4e13.4,f8.2)")
iyd = 10000
d = 9.999e-38
t = 294.1
println(format.write((iyd, d, d, d, d, t)))

```

which outputs

```julia
  10000 0.9999E-37 0.9999E-37 0.9999E-37 0.9999E-37 294.10

```

You would need to install PyCall with a Python environment that has the [fortranformat](https://pypi.org/project/fortranformat/) package installed. The details on how to do that depend on what OS you are using and whether you already have a Python environment installed.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [March 17, 2025, 3:16pm UTC](https://discourse.julialang.org/t/replicating-formatting-from-fortran-code-output/127036/5 "2025-03-17T15:16:04Z")

</div>

> [@trung](#):
>
> I don’t see any way to print it as `0.9999E-37`

Why does it matter?

---

<div class="post-metadata">

### Author: ![trung](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trung/32/49217_2.png) [@trung](https://discourse.julialang.org/u/trung)
#### Post date: [March 17, 2025, 3:24pm UTC](https://discourse.julialang.org/t/replicating-formatting-from-fortran-code-output/127036/6 "2025-03-17T15:24:01Z")

</div>

I want to be able to use `diff` in the command line for the Fortran output and the Julia output to compare them quickly instead of having to read in the Fortran output and compare both outputs in a Julia code

---

<div class="post-metadata">

### Author: ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)
#### Post date: [March 17, 2025, 3:37pm UTC](https://discourse.julialang.org/t/replicating-formatting-from-fortran-code-output/127036/7 "2025-03-17T15:37:38Z")

</div>

> [@trung](#):
>
> I want to be able to use `diff` in the command line for the Fortran output and the Julia output to compare them quickly

Sounds like you want functionality similar to [numdiff](https://github.com/tjhei/numdiff) or [spiff](http://hpux.connect.org.uk/hppd/hpux/Text/spiff-1.0/).
