Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to use UDT in Visual Basic to depict COBOL Decimal Comp-3?

Status
Not open for further replies.

simonye

Programmer
Mar 14, 2003
1
CA
how to use UDT in Visual Basic to depict COBOL Decimal Comp-3?

COBOL Decimal Comp-3(precision, scale, size) are list in the following.
(15,5,8) (9,0,5) (11,0,6) (17,5,9) (11,2,6) (3,0,2) (13,5,7) (7,0,4) (11,5,6)

For example, in the Invent.btr(This btr file come from business vision), the definition of fields is following:

(Field, Data Type, Size, Precision, Scale)
(I_WHSE,Char,2)
(I_PART_NO,Char,15)
(I_DESCRIPTION,Char,40)
…..
(I_COST_CURRENT,Decimal,8,15,5)
…..
I use the UDT below in Visual Basic:

Public Type InvFull_Fld
I_WHSE As String * 2
I_PART_NO As String * 15
I_DESCRIPTION As String * 40
I_Filler1 As String * 16
I_COST_CURRENT As Double
I_Filler2 As String * 967 ' total 1048
End Type

Note I use double data type to depict I_COST_CURRENT field, this field is a decimal(15,5,8) field (here 15 is precision , 5 is scale, and the betrieve size is 8). but when I use this UDT to get the record with Betrieve API, the result is wrong, for example, actually the original data is 0.00000 , and what I got is 6.98350748929955E-251
Thanks.

 
This is certainly not a double in the UDT. You will need to construct a translation between the Cobol format and a VB data type.

Now, I don't have any access to a Cobol-formatted DB, but... since the DataType is Decimal, this should not be too difficult to translate.

Make your UDT for this field a STRING*8, then translate the value with:

Function TranslatePackedDecimal(pd as String, Scale as Integer) as Double
Dim I as Integer
Dim Work As String
Dim B As Byte

For I = 1 to Len(pd)
B = Asc(Mid$(pd,I,1))

If I = Len(pd) Then
If ((B And 240) / 16) = 13 Then ' Negative Sign nibble
Work = "-" & Work
End If
Else
Work = Work & ((B And 240) / 16)
End If
Work = Work & (B And 15)
Next I

TranslatePackedDecimal = CDbl(Work)
If Scale <> 0 Then
TranslatePackedDecimal = TranslatePackedDecimal / (10^Scale)
End If
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top