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

Formatting floating point numbers into a string

martin_dietiker
Explorer
0 Likes
7,611

Hi,

I am relatively new in ABAP and I am searching for a FM or function to convert a given floating point number into a string using a given format (like using the [DecimalFormatter|http://java.sun.com/javase/6/docs/api/java/text/DecimalFormat.html] class in Java).

eg.

Number: 1234.56

Format: #,##0.00

Result 1,234.45

Number: 1234

Format #,##0.00

Result: 1,234.00

Number 0.34

Format: #,###.##

Result: .34

Number: 0.34

Format: #,##0.00

Result. 0.34

Number: 0.56

Format: 0.0

Result: 0.6

Number: 1234.56

Format: abc#,###

Result: abc1,235

and so on

I have already tried the 'using mask' expression in the write command and also the FM 'C14N_NUMERIC_FORMAT' but without my desired results.

Any idea

regards Martin

5 REPLIES 5
Read only

Former Member
0 Likes
3,442

Hi,

You can use WRITE....TO statement to convert. which converts the contents of the source field into a field with type C

WRITE <f1> TO <f2> <Options>

Eg:

DATA: NUMBER TYPE F VALUE '4.3',

TEXT(10),

FLOAT TYPE F,

PACK TYPE P DECIMALS 1.

WRITE NUMBER TO TEXT EXPONENT 2.

WRITE NUMBER TO FLOAT.

WRITE NUMBER TO PACK.

Regards

Shiva

Edited by: Shiva Kumar Tirumalasetty on Aug 24, 2009 8:03 PM

Read only

0 Likes
3,442

Thx Shiva,

but this dosn't solve my problem. I 'simply' want to apply a format like described above to the number and thus get the number in the desired format.

eg.

I have a float number 1234.34 which is stored in the float field like 1.23456E+3. I have a format telling me to show only the integer part of this number (like 1234). My idea is to apply a format '###9' to that number and it will give back a string containing '1234'. Likewise with the other format expressed in my original question.

hope this makes my question a bit more understandable.

regards

Martin

Read only

0 Likes
3,442

I've never seen that. In SAP, we don't bother with formats, we usually have requirements which require only one format. I'm afraid that you have to code it yourself.

Read only

0 Likes
3,442

Use 'FLTP_CHAR_CONVERSION' in this you can mention the number of decimal place you want to have...

Read only

retired_member
Product and Topic Expert
Product and Topic Expert
0 Likes
3,442

Nowadays, you format numeric data into strings with embedded expressions in string templates and their formatting options:

Embedded expressions - format options

WRITE TO is the old fashioned way ...