Solved

how to convert an integer to packed decimal in java

Posted on 2009-03-30
10
695 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
Comment Utility
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
Comment Utility
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
Comment Utility
You might try the Microfocus COBOL library or JRecord:

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

Author Comment

by:rishirajm
Comment Utility
I do not have to work with cobol file, however I need to convert an integer defined in java to packed decimal.
 
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 15

Expert Comment

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

Expert Comment

by:CEHJ
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
That's good, now you have two - as you can see mine works ;-)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hash value 2 38
Java array passed to SQL where clause 23 39
backtracking recursion  code 19 40
Java DateChooser? 2 0
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

743 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

18 Experts available now in Live!

Get 1:1 Help Now