How to handle scientific representations?

This space is dedicated to SAP users - administrators and programmers who want to integrate PTV Developer (native), the PTV xServers or the SAP Hana Spatial Services (HSS). Topics which require specific SAP based knowhow or which deal with SAP based mechanisms will be collected here.
Post Reply
User avatar
Bernd Welter
Site Admin
Posts: 3014
Joined: Mon Apr 14, 2014 10:28 am
Contact:

How to handle scientific representations?

Post by Bernd Welter »

Some SAP driven parties seem to struggle with the double values returned by our services (happens in xServer and Developer, e.g. within geocoding), (e.g. Greenwich) when they are formatted in the scientific style:

Code: Select all

"roadAccessCoordinate": {
          "x": 7.0078042365E-05,
          "y": 51.484439867
        },
        "address": {
          "country": "United Kingdom",          "state": "England",          "province": "London",
          "postalCode": "SE10 9NY",         "city": "London",          "district": "Greenwich",
          "subdistrict": "",          "street": "Old Woolwich Road",          "houseNumber": "78"
        },
Well - here's some advice from the internet:

To enable an SAP system (like SAP ABAP) to parse a scientific notation value (e.g., `1.23E+02` for `123`) from a JSON response and transform it into a Double, you can apply these strategies:
  1. Parse the JSON Response in ABAP
    If you have the JSON response directly in ABAP, you can use a JSON parser to read it. In SAP ABAP, classes like `CL_TREX_JSON_PARSER` (for older versions) or `CL_SXML_JSON_READER` (in newer versions) are available for JSON parsing.
  2. Extract the Scientific Notation Value as a String
    The scientific notation value in JSON is typically stored as a string. Read this string into an ABAP variable, such as `lv_scientific`.
  3. Convert the String to a Double Value
    ABAP supports converting scientific notation to numeric types like `FLOAT` (double). To achieve this, you can use the `CONV` or `VALUE` statements.

    Here’s an example:
    ```abap
    DATA: lv_scientific TYPE string VALUE '1.23E+02', " Example value
    lv_double TYPE f. " FLOAT type in ABAP

    " Convert the scientific notation string to Double (FLOAT)
    lv_double = CONV f( lv_scientific ).

    " Display result
    WRITE: / 'The double value is:', lv_double.
    ```
  4. Example with JSON Parsing

    If you have a JSON response that includes the scientific notation value, you can parse it and convert it to a double value as shown below:
    ```abap
    DATA: json_string TYPE string VALUE '{"value": "1.23E+02"}', " Example JSON
    json_reader TYPE REF TO cl_sxml_json_reader,
    lv_value TYPE string,
    lv_double TYPE f.

    " Instantiate JSON parser
    CREATE OBJECT json_reader
    EXPORTING
    json = json_string.

    " Extract the value
    json_reader->next_node( ).
    json_reader->next_node( ).
    json_reader->get_value( RECEIVING value = lv_value ).

    " Convert the value to Double
    lv_double = CONV f( lv_value ).

    " Display result
    WRITE: / 'The double value is:', lv_double.
    ```
  5. Important Notes

    - The `f` type in ABAP is equivalent to a Double (64-bit floating-point), which supports scientific notation.
    - If the scientific notation is not processed correctly, ensure it is in a valid format. SAP ABAP expects the notation to be in the form `1.23E+02`, where the letter "E" is uppercase.

    With this approach, you should be able to transform scientific notation values from a JSON response into a double value in SAP ABAP.
Bernd Welter
Technical Partner Manager Developer Components
PTV Logistics - Germany

Bernd at... The Forum,LinkedIn, Youtube, StackOverflow
I like the smell of PTV Developer in the morning... :twisted:
User avatar
MISTERX
Posts: 59
Joined: Mon Jun 02, 2014 11:50 am

Re: How to handle scientific representations?

Post by MISTERX »

Hello Bernd!

First of all, I would like to confirm the solution you have provided. It is detailed and complete - thank you for that!

In addition, I would like to refer to a post of mine in this forum in which I have dealt with the coordinate formats of the xServer and the PTV-Developer. This may be of interest to others: Available Coordinate Formats
Mr. X
  • Integration PTV <> SAP
  • Senior Consultant

________________________________________________

Optimism is only a lack of data!
Post Reply