How to handle scientific representations?
Posted: Wed Nov 13, 2024 11:07 am
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:
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:
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"
},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:
- 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. - 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`. - 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.
``` - 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. - 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.