Skip to content

Latest commit

 

History

History
355 lines (235 loc) · 5.14 KB

File metadata and controls

355 lines (235 loc) · 5.14 KB

Simple Conversion Between Two Units

Use method UNIT_CONVERSION_SIMPLE to convert values from one measurement unit to another and round the result to the number of decimal places maintained in the measurement unit table T006, if necessary. Depending on the parameter ROUND_SIGN, the rounding is up ('+'), down ('-'), commercial ('X''), or no rounding (SPACE).

Note:

Make sure that both units are maintained in the measurement unit table and are not dimensionless, but have both the same dimension.


Parameter Name

Value Help

INPUT

Input value

NO_TYPE_CHECK

Conversion factor type check

'X': No check

Space: Type check

ROUND_SIGN

Rounding flag

'+': Up

'-': Down

'X': Commercial

UNIT_IN

Unit of input value


Parameter Name

Value Help

ADD_CONST

Additive constant for conversion

DECIMALS

Number of decimal places for rounding

DENOMINATOR

Denominator for conversion

NUMERATOR

Numerator for conversion

OUTPUT

Output value


Exception Name

Value Help

CONVERSION_NOT_FOUND

Conversion factor could not be determined

DIVISION_BY_ZERO

Division by zero caught

INPUT_INVALID

Input value is not a number

OUTPUT_INVALID

Output parameter is not a number

OVERFLOW

Field overflow

TYPE_INVALID

Output parameter is not a number

UNITS_MISSING

No units specified

UNIT_IN_NOT_FOUND

UNIT_IN is not maintained

UNIT_OUT_NOT_FOUND

UNIT_OUT is not maintained

Sample Code:

CLASS zcl_uom_conversion DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC.
 
  PUBLIC SECTION.
     INTERFACES if_oo_adt_classrun.
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.
 
CLASS zcl_uom_conversion IMPLEMENTATION.
 
  METHOD if_oo_adt_classrun~main.
   DATA: lv_input  TYPE p DECIMALS 8 VALUE '1.98678923',
         lv_result TYPE p DECIMALS 8. 
    DATA(lo_unit) = cl_uom_conversion=>create( ).
 
    lo_unit->unit_conversion_simple( EXPORTING  input                = lv_input
                                                round_sign           = 'X'
                                                unit_in              = 'KG'
                                                unit_out             = 'G'
                                     IMPORTING  output               = lv_result
                                     EXCEPTIONS conversion_not_found = 01
                                                division_by_zero     = 02
                                                input_invalid        = 03
                                                output_invalid       = 04
                                                overflow             = 05
                                                units_missing        = 06
                                                unit_in_not_found    = 07
                                                unit_out_not_found   = 08 ).
    IF sy-subrc = 0.
      out->write( lv_result ).
    ENDIF.
  ENDMETHOD.
ENDCLASS.

For information about the built-in function UNIT_CONVERSION on CDS view level, see Handling of Amounts and Quantities in the ABAP Data Models Guide, and CDS DDL - CDS View Entity, Unit and Currency Conversion Functions in the ABAP Keyword Documentation.