Need to convert decimal value to packed decimal using c#
-
יום שישי 28 דצמבר 2007 13:52
I need to convert decimal to packed decimal value using c#. Is there .Net code or function out there to do the conversation?
The fields that needs to be packed decimal is the following format PIC S9(13)V99.
For example: If I have a transaction amount for 13 dollars. What will the actual packed values looks like?Thanks for the assistance in advance
כל התגובות
-
שבת 29 דצמבר 2007 23:28Are you generating a text file?
-
יום שלישי 01 ינואר 2008 22:25
Yes I am generating a text file. I need to send to it the mainframe.
Thanks
-
יום חמישי 03 ינואר 2008 05:48
13.00 dollars would be represented as 00000000000130{ using PIC S9(13)V99
I think that's what you are trying to do, yes?
If so, here are two VB.NET functions I wrote that convert a Double to/from a COBOL Signed Number. There may be a more elegant way to do this, but these work for me.
Code Block' Example converting a double to PIC S9(13)V99
Dim n As Double = -1234567890123.12
Dim output As String = ConvertFromDoubleToCOBOLSignedNumber(n, 15, 2)
MsgBox output
The second parameter is the number of 9's (digits) in the PIC. The third parameter is the number of implied decimal places.
The second function converts a COBOL Signed Number back to a Double.
That should hopefully help you out.Code Block' Displays -1234567890123.12
MsgBox ConvertFromCOBOLSignedNumberToDouble("12345678901231K", 2)
Gordon Bell
VBer since 1.0
Code BlockPublic Function ConvertFromDoubleToCOBOLSignedNumber(ByVal doubleNumber As Double, ByVal padLength As Integer, ByVal decimalPlaces As Integer) As String
' COBOL Signed Number Converter
Dim isNegative As Boolean = False
Dim codeCharsPositive As String = "{ABCDEFGHI"
Dim codeCharsNegative As String = "}JKLMNOPQR"
' Determine if number is negative, and remove sign
If doubleNumber < 0 Then
isNegative = True
doubleNumber = -doubleNumber
End If
' Offset decimal places
If decimalPlaces > 0 Then
doubleNumber = doubleNumber * (10 ^ decimalPlaces)
End If
' Assign to string
Dim signedNumber As String = doubleNumber.ToString
If signedNumber.IndexOf(".") > -1 Then
' Truncate remaining decimal places
signedNumber = signedNumber.Substring(0, signedNumber.IndexOf("."))
End If
' Pad with leading zeros
If signedNumber.Length < padLength Then
signedNumber = signedNumber.PadLeft(padLength, "0"c)
End If
' Tokenize last digit to encoded EBCIDIC character to specify sign of the number
' For Negative Numbers, the last digit is converted from
' 0123456789 to }JKLMNOPQR respectively.
' For Positive Numbers, the last digit is converted from
' 0123456789 to {ABCDEFGHI respectively.
Dim lastDigit As Integer = Convert.ToInt32(signedNumber.Substring(signedNumber.Length - 1, 1))
If isNegative Then
signedNumber = signedNumber.Substring(0, signedNumber.Length - 1) & codeCharsNegative.Substring(lastDigit, 1).ToString
Else
signedNumber = signedNumber.Substring(0, signedNumber.Length - 1) & codeCharsPositive.Substring(lastDigit, 1).ToString
End If
Return signedNumber
End Function
Public Function ConvertFromCOBOLSignedNumberToDouble(ByVal signedNumber As String, ByVal decimalPlaces As Integer) As Double
' COBOL Signed Number Converter
Dim codeChar As String
Dim convertedNumber As Double
Dim codeCharsPositive As String = "{ABCDEFGHI"
Dim codeCharsNegative As String = "}JKLMNOPQR"
signedNumber = signedNumber.Trim
If signedNumber = String.Empty Then
Return 0
End If
codeChar = signedNumber.Substring(signedNumber.Length - 1)
If codeCharsPositive.IndexOf(codeChar) > -1 Then
' For Positive Numbers, the last digit is converted from
' "{ABCDEFGHI" to "0123456789" respectively.
signedNumber = signedNumber.Substring(0, signedNumber.Length - 1) & codeCharsPositive.IndexOf(codeChar)
convertedNumber = Convert.ToDouble(signedNumber)
ElseIf codeCharsNegative.IndexOf(codeChar) > -1 Then
' For Negative Numbers, the last digit is converted from
' "}JKLMNOPQR" to "0123456789" respectively.
signedNumber = signedNumber.Substring(0, signedNumber.Length - 1) & codeCharsNegative.IndexOf(codeChar)
convertedNumber = -Convert.ToDouble(signedNumber)
Else
' Not a COBOL Signed Number, but still apply decimal places
convertedNumber = CDbl(signedNumber)
End If
If decimalPlaces > 0 Then
Return convertedNumber / (10 ^ decimalPlaces)
Else
Return convertedNumber
End If
End Function -
יום חמישי 03 ינואר 2008 23:31
That is exactly that what I am looking for.
Thanks
-
יום ראשון 11 מרץ 2012 21:49
HI
i'm trying to do the oppsite to this so i'm getting a PIC S9(13)V99 and i want to convert that to a number +13 i also want to convert for Example a 0000000567} to whatever number that equvlent to lets say -235 from a table . the script going to run in SSIS