具有前导零的浮点输出格式 [英] Floating point output format with leading zeros

查看:201
本文介绍了具有前导零的浮点输出格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我做出这些定义:

$ pre $ 实际参数:: No_01 = 2.34
实数, parameter :: No_02 = 34.56

我想用 F 格式:

 字符(*),参数:: FMT_01 ='(2x, F4.2)'
字符(*),参数:: FMT_02 ='(2x,F5.2)'

写入屏幕的结果是:

 写入(*,FMT_01)NO_01 => 2.34 
写入(*,FMT_02)NO_02 => 34.56

是否有任何形式的 F 格式这可以用于创建这种写作结果:
结果是:

 !正确的结果!不正确的结果
002.340东西2.340东西
034.560东西34.560东西

如何用前导零填充浮点输出?因为这些值可能是负值。

解决方案

对于不带前导零的常量字段宽度,你可以指定一个具有假定的最大字段宽度的格式。负号会自动处理,并且默认情况下这些值是右对齐的。例如:

 字符(len = 6):: form 
real :: rv1,rv2

!格式:浮点数,字段宽度= 8(包括。),小数位数= 2
form ='(f8.2)'
rv1 = -12.34
rv2 = 123.45

写(*,表格)rv1
写(*,表格)rv2

!示例输出。注意到两个领先的空间只是空白,但
!字段宽度(8)是常数,并且(2)小数位对齐:
-12.34
123.45

如果要使用前导零的常量字段宽度,则必须使用这个SO答案,以及您在问题中已经链接到的问题,以及@agentp在评论中提出的技巧。我不知道你是否理解,所以这里有一个子例程来演示这些想法:

 子程序leadingzeros(rv)
真实,意图(in):: rv
!局部变量
字符(len = 11):: form
$ b $ if(int(rv)> 0)then
form ='(i4.4,f0.2) '!总共允许4个前导零。
else
form ='(i4.3,f0.2)'! - 符号占用一个空间,所以剩下3个前导零。
endif

!处理负值,如agentp
写入(*,form)int(rv),abs(rv-int(rv))
结束子程序leadzeros

现在,当您调用 leadingzeros 时,输出如下所示:

 ! 'call leadingzeros(rv1)'
-012.34
的示例输出! 'call leadingzeros(rv2)'
0123.45


For example, if I make these definitions:

Real, parameter :: No_01 = 2.34
Real, parameter :: No_02 = 34.56

and I want to write these variables with F format in this way:

Character(*), parameter :: FMT_01 = '(2x,F4.2)'
Character(*), parameter :: FMT_02 = '(2x,F5.2)'

The result of writing to screen would be:

Write(*, FMT_01 ) NO_01 => 2.34
Write(*, FMT_02 ) NO_02 => 34.56

Is there any kind of F format which can be used for geting this result of writing instead: Result is:

!Correct result          !Not correct result
002.340 Something         2.340 Something
034.560 Something         34.560 Something

The answers to How to pad floating point output with leading zeros? are not applicable, because the values can be negative.

解决方案

For a constant field width without leading zeros, you could just specify a format with an assumed maximum field width. The negative signs are automatically handled, and the values are right-adjusted by default. For example:

character(len=6) :: form
real :: rv1, rv2

! Format: floating point, field width=8 (includes the "."), decimal places=2
form = '(f8.2)'  
rv1  = -12.34
rv2  =  123.45

write(*,form) rv1
write(*,form) rv2

! example output. notice the two leading spaces are just blanks, but the 
! field width ("8") is constant and the ("2") decimal places are aligned:
  -12.34
  123.45

If you want to have a constant field width with leading zeros, then you must use the technique shown at this SO answer you've already linked to in your question, along with the trick suggested by @agentp in a comment. I can't tell if you quite understood, so here's a subroutine demonstrating these ideas:

subroutine leadingzeros(rv)
    real, intent(in) :: rv
    ! local vars
    character(len=11) :: form

    if (int(rv)>0) then
        form='(i4.4,f0.2)'   ! allow a total of 4 leading zeros.
    else
        form='(i4.3,f0.2)'   ! "-" sign takes up one space, so 3 leading zeros remain.
    endif

    ! handle negative values as suggested by agentp
    write(*,form) int(rv), abs(rv-int(rv))
end subroutine leadingzeros

Now, when you call leadingzeros the output looks like:

! example output from 'call leadingzeros(rv1)'
-012.34
! example output from 'call leadingzeros(rv2)'
0123.45

这篇关于具有前导零的浮点输出格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆