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: 

remove decimal places

daniel_humberg
Contributor
0 Kudos

this is probably very easy, but i couldn't find the information I need yet.

I have a field in my db-table with type P (length 9, decimal places 3).

when i copy a value with 3 decimal places (e.g. 1,234) into the field, I want to remove the last digit (4), so the result should be 1,23. How do I do this without changing the data type and lenght of the field?

1 ACCEPTED SOLUTION

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

If your field is defined as type p decimals 3, then that is the format that it will take in the database. If you want to display this value in a program a different way, then you should use the following logic.



Data: x type p decimals 3 value '1.234'.
Data: y type p decimals 2.

y = x.

Write:/ x.
Write:/ y.

Regards,

Rich Heilman

3 REPLIES 3

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

If your field is defined as type p decimals 3, then that is the format that it will take in the database. If you want to display this value in a program a different way, then you should use the following logic.



Data: x type p decimals 3 value '1.234'.
Data: y type p decimals 2.

y = x.

Write:/ x.
Write:/ y.

Regards,

Rich Heilman

former_member183804
Active Contributor
0 Kudos

Hello Daniel,

that depends wether you want to have truncation or not. If you would like to have the number rounded up you simply can define an intermeditate variable. Else the use of the statement trunc should help.

Best Regards

Klaus


program test.

write: / ' CAST'.  perform cast  using :  '1.011',  '2.036'.
write: / 'TRUNC'. perform trunc using :  '1.011',  '2.036'.


form cast using value type p.

  data:
    3decs type p length 9 decimals 3,
    2decs type p length 9 decimals 2.

  3decs = value.
  write : 3decs.

  3decs = 2decs = 3decs.
  write : 3decs.

endform.

form trunc using value type p.

  data:
    3decs type p length 9 decimals 3.

  3decs = value.
  write : 3decs.

  3decs = ( trunc( 100 * 3decs ) / 100 ).
  write : 3decs.

endform.

andreas_mann3
Active Contributor
0 Kudos

Hi Daniel,

try this: WRITE:/ x decimals 2.

Andreas