将 FOR 与 REDUCE 一起使用时,ABAP 停止舍入

ABAP stop rounding when using FOR with REDUCE

我正在尝试使用 FOR 和 REDUCE 求和...当使用 FOR 和 REDUCE 时我们如何停止舍入... 示例 404120.71 四舍五入为 404121.000

data(lv_total_sum) = reduce tslxx9( 
      init x = 0 
      for wa in lt_table
      next x = x + wa-zzamount ).

INIT“块”中的变量是显式或隐式键入的。

在你的例子中,你分配了 0 所以它隐式分配了整数类型(这就是为什么你没有得到小数):

data(lv_total_sum) = reduce tslxx9( init x = 0 for wa in lt_table next x = x + wa-zzamount ).

如果你想显式分配一个类型,你可以这样做:

data(lv_total_sum) = reduce tslxx9( init x TYPE tslxx9 for wa in lt_table next x = x + wa-zzamount ).

使用TYPE,变量默认赋初值

有关 REDUCE 的更多信息,请参阅 ABAP documentation

编辑:补充解决方案:

  • 在某些情况下,您可能想要分配一个非初始值,例如下面的 10,然后您可以使用 CONV 分配一个类型:
    data(lv_total_sum) = reduce tslxx9( init x = CONV tslxx9( 10 ) for wa in lt_table next x = x + wa-zzamount ).
    
  • 在处理小数时,你可以简单地选择尽可能大的数字小数类型,即decfloat34(你也可以选择较小的,decfloat16):
    data(lv_total_sum) = reduce tslxx9( init x TYPE decfloat34 for wa in lt_table next x = x + wa-zzamount ).