Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to remove zeroes after the decimal point with minimum of 2 decimal places

former_member806437
Participant
0 Kudos
8,198

Hi,

Could you please help me how to remove the zeroes after the decimal point with minimum of 2 decimal places.

For ex: If the value is 12.0000, then the output has to be 12.00

If the value is 3,658.0020, then the output has to be 3,658.002.

Thanks in advance!!

8 REPLIES 8

thomas_jung
Developer Advocate
Developer Advocate
7,305

In ABAP, you can use the CONVERT function to remove the zeroes after the decimal point and round the value to the specified number of decimal places. Here is an example of how you can use the CONVERT function to remove the zeroes after the decimal point with a minimum of 2 decimal places:

DATA: lv_value TYPE p DECIMALS 2 VALUE '12.0000'.

lv_value = CONVERT #( lv_value, '.', ',' ).

In this example, the CONVERT function is used to convert the decimal point (.) to a comma (,), and the DECIMALS option is used to specify that the value should be rounded to 2 decimal places.

You can also use the ROUND function to round the value to the specified number of decimal places before using the CONVERT function. Here is an example of how you can use the ROUND and CONVERT functions together to remove the zeroes after the decimal point with a minimum of 2 decimal places:

DATA: lv_value TYPE p DECIMALS 2 VALUE '12.0000'.

lv_value = ROUND( lv_value, 2 ).
lv_value = CONVERT #( lv_value, '.', ',' ).

In this example, the ROUND function is used to round the value to 2 decimal places, and the CONVERT function is used to convert the decimal point (.) to a comma (,).

For example, to round the value 3,658.0020 to 2 decimal places, you can use the following code:

DATA: lv_value TYPE decfloat16 VALUE '3658.0020'.

lv_value = ROUND( lv_value, 2 ).

This will result in the value 3,658.002, which meets the requirements of having a minimum of 2 decimal places and no trailing zeroes.

For example, to format the value 3,658.0020 as a string with 2 decimal places, you can use the following code:

DATA: lv_value TYPE decfloat16 VALUE '3658.0020',
lv_formatted TYPE string.

lv_formatted = FORMAT( lv_value, '##0.00' ).

This will result in the string '3658.002', which again meets the requirements of having a minimum of 2 decimal places and no trailing zeroes.

Sandra_Rossi
Active Contributor
0 Kudos
7,305

You don't explain the exact context like the data types, so it's difficult to help precisely, there can be many different solutions.

I would simply output the number and remove all trailing 0 characters among the last 3 digits:

REPORT.
PARAMETERS p_num TYPE string DEFAULT '3658.00200'.
REPLACE REGEX '0{1,3}$' IN p_num WITH ''.
WRITE p_num.

3658.00200 will output 3658.002

3658.10000 will output 3658.10

0 Kudos
7,305

Hi Rossi,

Thanks for your reply.

May I know the syntax if the data type is lgmng/netwr/fkimg.

When data type is given as string the delimiter is getting removed but the delimiter is also required.

0 Kudos
7,305

I see that 2 fields among lgmng/netwr/fkimg have 3 decimals (the other one has 2 decimals), so you need to remove one zero.

I guess "delimiter" means the "thousands separator character".

REPORT.
PARAMETERS p_num TYPE fkimg DEFAULT '3658.002'.
DATA(num_as_text) = |{ p_num NUMBER = USER }|. " decimals separator, thousands separator
REPLACE REGEX '0{1,1}$' IN num_as_text WITH ''.
WRITE num_as_text.

NB: NUMBER = USER

0 Kudos
7,305

Hi Rossi,

I am getting this error. Could you please help me in this.

0 Kudos
7,305

What happens if you run MY program?

Just fix yours, the expression must be a number.

matt
Active Contributor
0 Kudos
7,305

sandra.rossi I think this error indicates that kunimbedu30 is running on a version of ABAP that doesn't have NUMBER = USER in string templates.

raymond_giuseppi
Active Contributor
7,305

SAP provide some special types for amount fields, those fields are associated to a currency code which provides the number of decimals (most currencies carry 2 decimals but some carry 0 or 3 decimals) - read WRITE curr URRENCY cuky and Currency Fields and Quantity Fields

WRITE BSEG-WRBTR TO <TEXT> [NO-GROUPING] CURRENCY BKPF-WAERS.

When Dynpro or ALV field catalog specifiy the link betweeen amount and currency code (ddic), SAP will perform automatically the conversion at display.

(A similar solution exists for units of measurement.)