• Post Reply Bookmark Topic Watch Topic
  • New Topic

BigDecimal - Appending Leading Zeros  RSS feed

 
Konda Golamaru
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to convert a COBOL code into Java, have an issue converting BigDecimal into byte array.
The COBOL filed type is: // PIC S9(12)V9(6) (total length 18, integer part: 12, scale: 6)

The COBLO program returns the data for the above field: 12.332234 which is BigDecimal in Java. I need to append leading zeros(this case: 10) and convert into byte array.
The the final BigDecimal value should look like: 000000000012.332234. I converted BigDecimal into String, added leading zeros. When I contert into back to BigDecimal, all leading zeros are gone. Can any body help in regad this.

Thanks,
Konda.
// --- F
 
surlac surlacovich
Ranch Hand
Posts: 296
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a difference between what you STORE, and what you DISPLAY. BigDecimal stores a value. the value is 12.332234. you cannot store leading zeros in there, because that doesn't make sense for the value.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:There is a difference between what you STORE, and what you DISPLAY. BigDecimal stores a value. the value is 12.332234. you cannot store leading zeros in there, because that doesn't make sense for the value.

Furthermore, if I remember my COBOL right (it's been 20 years ), PIC S9(12)V9(6) is a character format, not a numeric one; otherwise it would have had COMP or COMP-3.

However, what the others have said would appear to be your actual problem.

Winston
 
Konda Golamaru
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Winston/Fred,

Thanks for the reply. Infact the COBOL field is COMP-3. // PIC S9(12)V9(6) // --- COMP-3
I have BigDecimal field in Java with value say 12.343434. so I want to convert into PackedDecimal.

Thanks,
Konda.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trying to convert COBOL to Java at the implementation level is going to be a difficult proposition. It's like trying to translate Chinese to English word-for-word; you end up with strange constructs in the target language (E.g. "I said this to you, correct or not?") There are just some constructs that don't translate directly from one language to another. My advice would be to understand the INTENT of the COBOL program, then program that same intent in Java without taking too much of the COBOL implementation and trying to literally translate it to Java. BTW, the intent of the weird translation ("I said this to you, correct or not?") was to simply ask "Didn't I tell you this?"

As pointed out earlier, the intent of having leading zeros was related to the display of the value, whereas the BigDecimal class is concerned with storing the value. Find something else that matches the intent in COBOL, like a formatter.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Konda Golamaru wrote:Thanks for the reply. Infact the COBOL field is COMP-3. // PIC S9(12)V9(6) // --- COMP-3
I have BigDecimal field in Java with value say 12.343434. so I want to convert into PackedDecimal.

Are you sure?

Your original post said that you want to "convert a COBOL code into Java", by which I thought you meant converting the process from COBOL to Java, so - unless there are processes outside the one that you're converting that need to communicate specifically with your packed data - you're better off forgetting about COMP-3 altogether. It's an IBM construct that was used back in the '60's and '70's as a symbolic representation for numbers, and really doesn't have much use in a modern systems.

That said, it can be done, and I suspect that BigInteger would probably be the easiest way to to it, but not necessarily the best.

One more time: WHAT do you need to do?

If it's merely translation of a COBOL program, then stick to Java - and in the case you're talking about, I think you would be far better off to use BigDecimal than BigInteger.

If this Java program needs to communicate with other programs in packed-decimal format, then you're into the realms of data conversion, because PD simply doesn't exist in Java. It can be done, but it's a LOT more involved and, if input is involved, I'm not sure where you're going to get your "implied" decimal point, or indeed, style (it was always part of the program in COBOL):
PIC 9(6) COMP-3
produces a different encoding to
PIC S9(6) COMP-3

Winston
 
surlac surlacovich
Ranch Hand
Posts: 296
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Konda Golamaru wrote:
I'm trying to convert a COBOL code into Java, have an issue converting BigDecimal into byte array.

One question, what are your performance requirements? I heard that BigDecimal and BigInteger are slow.
Did anybody use GMP or MPFR, are there any real benefits (because one should use JNI to glue this libs)?
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
surlac surlacovich wrote: . . . I heard that BigDecimal and BigInteger are slow. . . .
But they are accurate. That is one of the reasons why you stand next to the cash machine for 5″ before it requests your password. Another reason is that passing the details across the net to the bank takes several seconds whereas BigDecimal calculations may take a few μs.

COBOL code was used for banking applications, so they probably require accuracy a speed. Also, how do you know your source is correct about performance?bove
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!