Application Development and Automation 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: 
Read only

Decimal values check

Former Member
0 Likes
7,528

Hi,

Given a string of character values, I get the values comprising of character values and also floating point values. eg 25.75 . The problem is the decimal value is not deciphered as a decimal but instead a character value. Could you please give me the name of the function module that would recognise the floating point value..

Thanks in advance....

14 REPLIES 14
Read only

Former Member
0 Likes
4,335

Hi sanjay,

1. Declare another variable

data : num type p decimals 2.

2. then just assign

num = mystring.

3. then use num in all your calculations.

4. eg (just copy paste in new program)

report abc.

data : mynum type p decimals 2.

data : mystr(15) type c.

mystr = '25.65'.

mynum = mystr.

mynum = mynum + 1.

write mynum.

regards,

amit m.

Message was edited by: Amit Mittal

Read only

vinod_gunaware2
Active Contributor
0 Likes
4,335

Hi

Convert char to number and then compare.

regards

vinod

Read only

hymavathi_oruganti
Active Contributor
0 Likes
4,334

declare a variable type p

and assign this vaue to that.

type casting will be done.

Read only

Former Member
0 Likes
4,334

Hi Sanjay,

Would you please be a little more specific about your requirement?

Still, from what I understand you want to convert the values stored ina character variable to a numeric value complete with the decimal places.

You can do so as follows:

DATA: WA_TMP TYPE P DECIMALS 2,
      WA_TXT(10) VALUE '24.54'.

WA_TMP = WA_TXT.
WRITE: WA_TMP.

This way you will be able to preserve the numbers after the decimal as well.

Hope this helps,

Regards,

Madhur

NB: Please award points if found helpful.

Read only

0 Likes
4,334

Hi Madhur,

eq 1) field xyz = 20.30.

2) field xyz = 'SAM_TEST' .

Thus xyz can have packed number or character value. If it is a packed number I can assign directly to any packed variable, However if it is 'SAM_TEST' value then i would have to search through another table and retrieve value for that string.

My problem is to find if xyz contains P value or character string.

Thanks in Adv

Read only

0 Likes
4,334

Hi again,

1. we can check like this.

2.

REPORT abc.

PARAMETERS : mystr(15) TYPE c.

start-of-selection.

IF mystr CO '1234567890. '.

WRITE 😕 'OK'.

else.

WRITE 😕 'incorrect value'.

ENDIF.

regards,

amit m.

Read only

0 Likes
4,334

Hi,

Maey be you can write code like below.

if field CO '0123456789.'.(Need to check . can be put in '')

  • it is charatcter

else.

  • it is a number

endif.

Message was edited by: Ramakrishna Prasad

Read only

0 Likes
4,334

Hi Sanjay,

You will have to parse the character variable character by character to determine if it contains any character field.

This can easily be achieved by the following code.

REPORT ZTMP.
DATA: WA_TMP TYPE P DECIMALS 2,
      WA_TXT(10) VALUE '35.56',
      WA_LTH TYPE I,
      WA_INDEX TYPE I VALUE 0,
      WA_INDC(1) VALUE ' '.

CONDENSE WA_TXT.
WA_LTH = STRLEN( WA_TXT ).

DO WA_LTH TIMES.
  IF WA_TXT+WA_INDEX(1) CO '0123456789.'.
    WA_INDC = 'X'.
  ELSE.
    WA_INDC = ' '.
    EXIT.
  ENDIF.
  WA_INDEX = WA_INDEX + 1.
ENDDO.

IF WA_INDC EQ ' '.
  WRITE: 'VARIABLE CONTAINS A STRING.'.
ELSE.
  WRITE: 'VARIABLE CONTAINS A PACKED DECIMAL.'.
ENDIF.

Using CO directly will not work.

Regards,

Madhur

                          • CORRECTION ***************

Sorry Sanjay,

I made a mistake here. Using CO directly will work if we include a SPACE " " character in our CO criteria. This would be as follows:

REPORT ZTMP .

DATA: WA_TMP TYPE P DECIMALS 2,
      WA_TXT(10) VALUE '35.57',
      WA_LTH TYPE I,
      WA_INDEX TYPE I VALUE 0,
      WA_INDC(1) VALUE ' '.

CONDENSE WA_TXT.

IF WA_TXT CO '0123456789. '. "This contains a space char.
  WRITE: 'VARIABLE CONTAINS A PACKED DECIMAL.'.
ELSE.
  WRITE: 'VARIABLE CONTAINS A STRING.'.
ENDIF.

Message was edited by: Madhur Chopra

Message was edited by: Madhur Chopra

Read only

0 Likes
4,334

Hi again,

To remind.

1. IF mystr CO '1234567890. '.

there is a SPACE after the . (DOT)

2. If we do not give space,

it won't work.

3. its like this.

IF mystr CO '1234567890.@'.

where @ = SPACE

regards,

amit m.

Read only

0 Likes
4,334

hi ,

you can just use.

data : typ(1).
  describe field xyz type typ.
 

typ contains C if character and P if it is packed.

regards

satesh

Read only

0 Likes
4,334

Hi Amit,

Yes, I just realised out that SPACE was necessary for this to work since its a character field defined with the specified length. If this length is not completely filled up, it will contains spaces and hence not work.

I made the necessary corrections to my post.

Thanks!

Regards,

Madhur

Read only

0 Likes
4,334

Hi Satesh,

Sanjay's requirement is to know the content of the variable. His variable XYZ is of type C, and can contain either a string or a decimal number. In case its a Decimal No. he wants to move it into another variable of type Packed Decimal.

Regards,

Madhur

Read only

0 Likes
4,334

hi madhur,

then i think this will do..

 parameters : c(10) type c.

data : c1(10) , p type p decimals 2.
<b> if c ca sy-abcde.</b>
  c1 = c.
   write : c1.
 else.
  p = c.
   write : p.
 endif.

regards

satesh

Read only

Former Member
0 Likes
4,334

hi again,

1. did your problem get solved ?

2. As per the forum etiquette,

u may pls award points

to helpful answers by clicking the STAR

on the left of that reply.

regards,

amit m.

regards,

amit m.