title: Formatted output with leading zeros in Fortran
body: <p>I have some decimal numbers that I need to write to a text file with leading zeros when appropriate.  I've done some research on this, and everything I've seen suggests something like:</p>
  
  <pre><code>  REAL VALUE
    INTEGER IVALUE
  
    IF (VALUE.LT.0) THEN
      IVALUE = CEILING(VALUE)
    ELSE
      IVALUE = FLOOR(VALUE)
    ENDIF
    WRITE(*,1) IVALUE, ABS(VALUE)-ABS(IVALUE)
  1 FORMAT(I3.3,F5.4)
  </code></pre>
  
  <p>As I understand it, the <code>IF</code> block and <code>ABS</code> parts should allow this to work for all values on -100 &lt; VALUE &lt; 1000.  If I set <code>VALUE = 12.3456</code>, the code above should produce "012.3456" as the output, and it does.  However if I have something like <code>VALUE = -12.3456</code>, I'm getting "(3 asterisks).3456" as my output.  I know the asterisks usually shows up when there are not enough characters provided for in the <code>FORMAT</code> statement, but 3 should be enough in this example (1 character for the "-" and two characters for "12").  I haven't tested this yet with something like <code>VALUE = -9.876</code>, but I'd expect the output to be "-09.8760".</p>
  
  <p>Is there something wrong in my understanding of how this works? Or is there some other limitation of this technique that I'm violating?</p>
  
  <p>UPDATE: Okay I've looked into this some more, and it seems to be a combination of a negative value and the <code>I3.3</code> format.  If VALUE is positive and I have the <code>I3.3</code>, it will put leading zeros as expected.  If VALUE is negative and I only have <code>I3</code> as my format, I get the correct value output, but it will be padded with spaces before the negative sign instead of padded with zeros after the negative (so -9.8765 is output as " -9.8765", but that leading space breaks what I'm using the .txt file for, so it's not acceptable).</p>