none
Need to convert decimal value to packed decimal using c# RRS feed

  • Question

  • 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

    Friday, December 28, 2007 1:52 PM

Answers


  • 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
    .

    Code Block

    ' Displays -1234567890123.12

    MsgBox ConvertFromCOBOLSignedNumberToDouble("12345678901231K", 2)


    That should hopefully help you out.

    Gordon Bell
    VBer since 1.0

    Code Block
        Public 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

    Thursday, January 3, 2008 5:48 AM

All replies

  • Are you generating a text file?

    Saturday, December 29, 2007 11:28 PM
  • Yes I am generating a text file.  I need to send to it the mainframe. 

     

    Thanks

     

    Tuesday, January 1, 2008 10:25 PM

  • 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
    .

    Code Block

    ' Displays -1234567890123.12

    MsgBox ConvertFromCOBOLSignedNumberToDouble("12345678901231K", 2)


    That should hopefully help you out.

    Gordon Bell
    VBer since 1.0

    Code Block
        Public 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

    Thursday, January 3, 2008 5:48 AM
  • That is exactly that what I am looking for. 

     

    Thanks

     

     

    Thursday, January 3, 2008 11:31 PM
  • 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  

    Sunday, March 11, 2012 9:49 PM