0

I am trying to convert a double value to Packed decimal COBOL format PIC S9(5)V9(4) COMP-3/Packed decimal COBOL format PIC S9(3)V9(4) COMP-3 format in JAVA, see below example :

Double Value : 00000.6775 Converted to Packed Decimal : ^@^FS,^@

Is this conversation possible in java?

Arjun
  • 38
  • 1
  • 1
  • 5
  • 1
    Yes, it is possible, but according to the tour you've taken this is neither a good answer nor is "please help me to achieve this" a good question for StackOverflow. As you now know that it is possible I suggest to give it a try, editing the question with the code you have (which shows not "now convert here" but a try on the conversion) allowing others to help. I suggest to first check how a comp-3 field is encoded (likely has to do with bytes) and then check how you convert those bytes. – Simon Sobisch Jun 07 '21 at 07:40
  • Would you please edit your question and remove dublicate words, and also show the expected result in a useful way (^@^FS,^@ is not useful) – phunsoft Jun 07 '21 at 12:10

1 Answers1

3

Similar questions have been answered multiple times already on Stack overflow

For s9(5)V9(4) comp-3, 123.45 is represented in byte format as

00 12 34 50 0c

The C is the sign

Yes this is possible, the JRecord package will do it (may truncate extra digits though).

The approach is

  • to Convert double to a unsigned and unscaled long / BigInteger i.e 123.45 --> 1234500 (for s9(5)v9(4))
  • Convert the long to packed decimal

At this point there are several approaches

Rick Smith
  • 3,962
  • 6
  • 13
  • 24
Bruce Martin
  • 10,358
  • 1
  • 27
  • 38