Skip to content

Commit ac92151

Browse files
committed
Add more options to control real number printing
- signed zeros and always print sign or suppress signs - general (G), exponential (E), scientific (ES) and Engineering (EN) formats allowed
1 parent 1fb0479 commit ac92151

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/json_module.F90

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,19 +1573,45 @@ end subroutine wrap_json_file_get_string_vec
15731573
!# Modified
15741574
! * Izaak Beekman : 02/24/2015
15751575

1576-
subroutine json_initialize(verbose,compact_reals)
1576+
subroutine json_initialize(verbose,compact_reals,print_signs,real_format)
15771577

15781578
implicit none
15791579

15801580
logical(LK),intent(in),optional :: verbose !! mainly useful for debugging (default is false)
15811581
logical(LK),intent(in),optional :: compact_reals !! to compact the real number strings for output
1582+
logical(LK),intent(in),optional :: print_signs !! always print numeric sign (default is false)
1583+
character(len=*,kind=CDK),intent(in),optional :: real_format
1584+
!! exponential (default), scientific, engineering or general
15821585

15831586
character(kind=CDK,len=10) :: w,d,e
1587+
character(kind=CDK,len=2) :: sgn, rl_edit_desc
15841588
integer(IK) :: istat
1589+
logical(LK) :: sgn_prnt
1590+
15851591

15861592
!clear any errors from previous runs:
15871593
call json_clear_exceptions()
15881594

1595+
!set defaults
1596+
sgn_prnt = .false.
1597+
if ( present( print_signs) ) sgn_prnt = print_signs
1598+
if ( sgn_prnt ) then
1599+
sgn = 'sp'
1600+
else
1601+
sgn = 'ss'
1602+
end if
1603+
1604+
rl_edit_desc = 'E'
1605+
if ( present( real_format ) ) then
1606+
select case ( real_format )
1607+
case ('g','G','e','E','en','EN','es','ES')
1608+
rl_edit_desc = real_format
1609+
case default
1610+
call throw_exception('Invalid real format, "' // trim(real_format) // '", passed to json_initialize.'// &
1611+
new_line('a') // 'Acceptable formats are: "G", "E", "EN", and "ES".' )
1612+
end select
1613+
end if
1614+
15891615
# ifdef USE_UCS4
15901616
! reopen stdout and stderr with utf-8 encoding
15911617
open(output_unit,encoding='utf-8')
@@ -1608,9 +1634,9 @@ subroutine json_initialize(verbose,compact_reals)
16081634
if (istat==0) write(d,'(ss,I0)',iostat=istat) real_precision
16091635
if (istat==0) write(e,'(ss,I0)',iostat=istat) real_exponent_digits
16101636
if (istat==0) then
1611-
real_fmt = '(ss,E' // trim(w) // '.' // trim(d) // 'E' // trim(e) // ')'
1637+
real_fmt = '(' // sgn // ',' // trim(rl_edit_desc) // trim(w) // '.' // trim(d) // 'E' // trim(e) // ')'
16121638
else
1613-
real_fmt = '(ss,E30.16E3)' !just use this one (should never happen)
1639+
real_fmt = '(' // sgn // ',' // trim(rl_edit_desc) // '30.16E3)' !just use this one (should never happen)
16141640
end if
16151641
end if
16161642

0 commit comments

Comments
 (0)