Home > Archive > Fortran > September 2006 > double to char
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Julian Bessenroth 2006-09-26, 8:04 am |
|
Hi folks,
I'm a bit . I need to convert a double precision into a
charakter sting of a certain length. [FORTRAN77]
I tried is like this
double precision dvalue
character*20 buffer
write(buffer,'(1pg12.7)') dvalue
But I get problems with doubles like 1.1234E-03 and so on. When I have
a look at buffer then it is just '*********...' .
There must not be any leading spaces in the char variable.
Does anyone habe a hint for me?
regards
Julian
| |
| David Flower 2006-09-26, 8:04 am |
|
Julian Bessenroth wrote:
> Hi folks,
>
> I'm a bit . I need to convert a double precision into a
> charakter sting of a certain length. [FORTRAN77]
>
> I tried is like this
>
> double precision dvalue
> character*20 buffer
>
> write(buffer,'(1pg12.7)') dvalue
>
> But I get problems with doubles like 1.1234E-03 and so on. When I have
> a look at buffer then it is just '*********...' .
>
> There must not be any leading spaces in the char variable.
>
> Does anyone habe a hint for me?
I think that you are close; try the FORMAT:
SP, 1PD20.13
(The SP forces '+' signs for avoid unwanted blanks)
Personally, I avoid G FORMAT.
Dave Flower
>
> regards
>
> Julian
| |
| Julian Bessenroth 2006-09-26, 8:04 am |
|
Hi David,
thanks a lot for you fast response!
David Flower schrieb:
> I think that you are close; try the FORMAT:
>
> SP, 1PD20.13
>
> (The SP forces '+' signs for avoid unwanted blanks)
hmm, that still does not work if i want the char to be somewhat shorter
write(buffer,'(SP, 1PD12.7)') value
it still ends up with ***** in the buffer. I actually need :
1.123456789012E-03 to be => '0.0011234'
most beautiful would be it would give '0.0011235', but that not so
important.
> Personally, I avoid G FORMAT.
If you have an idea on how I can avoid it ...
TIA
Julian
| |
| David Flower 2006-09-26, 8:04 am |
|
Julian Bessenroth wrote:
> Hi David,
>
> thanks a lot for you fast response!
>
> David Flower schrieb:
>
>
> hmm, that still does not work if i want the char to be somewhat shorter
>
> write(buffer,'(SP, 1PD12.7)') value
>
> it still ends up with ***** in the buffer. I actually need :
>
> 1.123456789012E-03 to be => '0.0011234'
>
> most beautiful would be it would give '0.0011235', but that not so
> important.
>
>
> If you have an idea on how I can avoid it ...
>
> TIA
>
> Julian
I assume therefore that trailing spaces are acceptable, If so, try:
F12.10, 8X (If in range (9.99999, 0.0)
If you want to avoid asterisks, try (SP, 1PD12.5)
Or you can get really sophisticated, and use different formats for
different values
Dave Flower
| |
| Julian Bessenroth 2006-09-26, 7:00 pm |
|
David Flower schrieb:
Hi David,
> I assume therefore that trailing spaces are acceptable, If so, try:
oh yes, I forget to mention that.
> F12.10, 8X (If in range (9.99999, 0.0)
>
> If you want to avoid asterisks, try (SP, 1PD12.5)
>
> Or you can get really sophisticated, and use different formats for
> different values
I think I will do it this way, I cant be sure to stay in the range
mentioned above.
Thanks a lot David!
regards
Julian
| |
| David Flower 2006-09-26, 7:01 pm |
|
Julian Bessenroth wrote:
> David Flower schrieb:
>
> Hi David,
>
>
> oh yes, I forget to mention that.
>
>
> I think I will do it this way, I cant be sure to stay in the range
> mentioned above.
>
You may find this SUBROUTINE useful, although it may be more powerful
than you require
David Flower
SUBROUTINE FORMAT_R8
A ( Var_Min, Var_Max, Defined, Invalid, Negative,
B FRMT, Siz )
C
C This is a utility routine to determine a FORMAT string that will
C handle REAL variables
C
IMPLICIT NONE
C
C Minimum value of ABS(VARS())
REAL*8 Var_Min
C Maximum value of ABS(VARS())
REAL*8 Var_Max
C Whether a valid number has to be output (i.e. Not all Nan etc.)
LOGICAL Defined
C Whether an invalid number has to be output (e.g.-Infinity)
LOGICAL Invalid
C Whether a negative number has to be output
LOGICAL Negative
C FORMAT string produced
CHARACTER*(*) FRMT
C Number of output characters produced by FRMT
C (e.g.If FRMT='(F9.2)', Siz=9)
INTEGER Siz
CC Location of leftmost non-zero digit in VAR_MAX
C 1 => immediately to left of decimal point
C 0 => immediately to right of decimal point
INTEGER NDMAX
C Location of leftmost non-zero digit in VAR_MIN
C Defined as for NDMAX
INTEGER NDMIN
C Number of digits required to right of decimal place
INTEGER NDEC
C
C Determine required number of digits on left and right
C of decimal point for F FORMAT output
IF ( DEFINED ) THEN
IF ( VAR_MAX .GT. 1.0 ) THEN
NDMAX = INT ( LOG10(VAR_MAX) + 1.000000000001D0 )
ELSE IF ( VAR_MAX .NE. 0.0 ) THEN
NDMAX = INT ( LOG10(VAR_MAX) + 0.000000000001D0 )
ELSE
NDMAX = 1
END IF
IF ( VAR_MIN .GT. 1.0 ) THEN
NDMIN = INT ( LOG10(VAR_MIN) ) + 1
ELSE IF ( VAR_MIN .NE. 0 ) THEN
NDMIN = INT ( LOG10(VAR_MIN) )
ELSE
NDMIN = 1
END IF
END IF
C
C Default output is 1PE25.17, so F format can only be used if the
C max-min spread does not exceed 5 orders of magnitude, and the extreme
C values can be output in F FORMAT
C
IF ( DEFINED ) THEN
IF ( NDMAX.GT.22 .OR.
A NDMIN.LT.-4 .OR.
B NDMAX-NDMIN .GT. 5 ) THEN
IF ( NEGATIVE ) THEN
Siz = 25
FRMT = '(1PE25.17)'
ELSE
Siz = 24
FRMT = '(1PE24.17)'
END IF
ELSE IF ( VAR_MAX.EQ.0.0 .AND. VAR_MIN.EQ.0.0 ) THEN
Siz = 3
FRMT = '(F3.1)'
ELSE
NDEC = MAX ( 1, 17-NDMIN )
IF ( NEGATIVE ) THEN
Siz = MAX(NDMAX,1) + NDEC + 2
ELSE
Siz = MAX(NDMAX,1) + NDEC + 1
END IF
IF ( NDEC .GE. 10 ) THEN
WRITE ( FRMT, 10 ) Siz, NDEC
10 FORMAT ( '(F', I2, '.', I2, ')' )
ELSE
WRITE ( FRMT, 20 ) Siz, NDEC
20 FORMAT ( '(F', I2, '.', I1, ')' )
END IF
END IF
ELSE
Siz = 9
END IF
IF ( INVALID ) THEN
Siz = MAX ( Siz, 9 )
END IF
C
RETURN
C
END
> Thanks a lot David!
>
> regards
>
> Julian
| |
| Tobias 2006-09-26, 7:01 pm |
| Hello,
Julian Bessenroth wrote:
>
> I think I will do it this way, I cant be sure to stay in the range
> mentioned above.
well, then use something like:
write(buffer,'(F18.10)') dvalue ! adapt to maximal length needed
buffer = adjustl(buffer) ! left justify text
Tobias
| |
| Michael 2006-09-26, 7:01 pm |
|
> write(buffer,'(1pg12.7)') dvalue
I use '1PG15.5E-2', it works good with big range of values.
| |
| Colin Watters 2006-09-26, 7:01 pm |
| "Julian Bessenroth" <jbusenet@gmx.de> wrote in message
news:1159267122.429104.87990@i3g2000cwc.googlegroups.com...
>
> Hi folks,
>
> I'm a bit . I need to convert a double precision into a
> charakter sting of a certain length. [FORTRAN77]
>
> I tried is like this
>
> double precision dvalue
> character*20 buffer
>
> write(buffer,'(1pg12.7)') dvalue
>
> But I get problems with doubles like 1.1234E-03 and so on. When I have
> a look at buffer then it is just '*********...' .
>
G format has an 'overhead' of 7; i.e., to avoid the sort of problems you
describe above, the total width (the 'xx' in Gxx.yy) must be greater than
the significant digits ('yy') by at least 7. Thus G14.7 is good, as is
G12.5, but your G12.7 is not.
Why 7? Good question. The exponent requires 4, then there's the decimal
point and space for a leading minus, and that makes 6. So why 7? Ducked if I
know, but that's the way its always been. Anyone else know?
Qolin
Email: my qname at domain
Domain: qomputing dot demon dot co dot uk
| |
| glen herrmannsfeldt 2006-09-26, 7:01 pm |
| Colin Watters <qolin.see_signature@nowhere.co.uk> wrote:
(someone wrote)
[color=darkred]
> G format has an 'overhead' of 7; i.e., to avoid the sort of problems you
> describe above, the total width (the 'xx' in Gxx.yy) must be greater than
> the significant digits ('yy') by at least 7. Thus G14.7 is good, as is
> G12.5, but your G12.7 is not.
> Why 7? Good question. The exponent requires 4, then there's the decimal
> point and space for a leading minus, and that makes 6. So why 7? Ducked if I
> know, but that's the way its always been. Anyone else know?
There is one digit before the decimal point, too. Without the 1P
that is zero. With 1P you get one significant digit before
the decimal point.
That should apply for E and D, and for G when printed
using E notation.
-- glen
| |
| Julian Bessenroth 2006-09-28, 4:00 am |
|
Many thanks to all of you! Now I've got two solutions (next thread ist
: Which to take *lol*).
Thanks a lot
regards
Julian
| |
|
| Julian Bessenroth wrote in message <1159267122.429104.87990@i3g2000cwc.googlegroups.com>...
>
>Hi folks,
>
>I'm a bit . I need to convert a double precision into a
>charakter sting of a certain length. [FORTRAN77]
>
>I tried is like this
>
> double precision dvalue
> character*20 buffer
>
> write(buffer,'(1pg12.7)') dvalue
>
>But I get problems with doubles like 1.1234E-03 and so on. When I have
>a look at buffer then it is just '*********...' .
>
>There must not be any leading spaces in the char variable.
You won't be able to avoid them.
Here is a range of values printed with 1PG13.7 [1PG12.7 produces *****
for some values] This format doesn't allow for negative values either.
1.2345000E-07
1.2345000E-06
1.2345000E-05
1.2345000E-04
1.2345000E-03
1.2345000E-02
0.1234500
1.234500
12.34500
123.4500
1234.500
12345.00
123450.0
1234500.
1.2345000E+07
1.2345000E+08
1.2345000E+09
So you will need to shift the contents of BUFFER to the left
{two assignments needed for that),
or to print BUFFER beginning with the first non-blank.
|
|
|
|
|