Solved

how to convert an integer to packed decimal in java

Posted on 2009-03-30
10
717 Views
Last Modified: 2012-06-27
can some one help to provide sample code for converting integer to packed decimal
0
Comment
Question by:rishirajm
  • 4
  • 3
  • 2
10 Comments
 
LVL 15

Expert Comment

by:fsze88
ID: 24025731
like this?
       System.out.println(get4DigitsFormat(4.323443));
        System.out.println(get4DigitsFormat(4.320));
        System.out.println(get4DigitsFormat(4));

    public  String get4DigitsFormat(double num) {
//        String format = "###,##0.0000";
        String format = "###,##0.####";
        DecimalFormat formatter = new DecimalFormat(format);
        num = (num == -0) ? Math.abs(num) : num; // to change -0 to 0
        return formatter.format(num);
    }
0
 

Author Comment

by:rishirajm
ID: 24026069
I need a logic to convert an integer to packed decimal format. please follow the url 'http://www.3480-3590-data-conversion.com/article-packed-fields.html' for packed decimal. thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24026499
You might try the Microfocus COBOL library or JRecord:

http://jrecord.sourceforge.net/JRecord04.html#Header_10
0
 

Author Comment

by:rishirajm
ID: 24027141
I do not have to work with cobol file, however I need to convert an integer defined in java to packed decimal.
 
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Expert Comment

by:fsze88
ID: 24027241
to simply way to using DecimalFormat and store the values into String object
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24027945
>>I do not have to work with cobol file

You'll still probably be able to use the source
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24029081
Try something like the following:
    public static int intToPackedDecimal(int val) {

        int result = 0xC;

        String sVal = Integer.toString(val);

        if (sVal.charAt(0) == '-') {

            sVal = sVal.substring(1);

            result = 0xD;

        }

        final int NYBBLE = 4;

        int nybbleIndex = 1;

        for(int i = sVal.length() -1;i >= 0;i--) {

            result |= ((sVal.charAt(i) - '0') & 0xF) << (NYBBLE * nybbleIndex++);

        }

        return result;

    }

Open in new window

0
 

Accepted Solution

by:
rishirajm earned 0 total points
ID: 24046031
Thanks CEHJ for your quick response. I was able to find solution at http://sourceforge.net/projects/zdecimal
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24047147
That's good, now you have two - as you can see mine works ;-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 80
thymeleaf natural templating vs JSP 2 69
eclipse argument 14 53
Java DateChooser? 3 29
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now