how to convert an integer to packed decimal in java

Posted on 2009-03-30
Last Modified: 2012-06-27
can some one help to provide sample code for converting integer to packed decimal
Question by:rishirajm
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
LVL 15

Expert Comment

ID: 24025731
like this?

    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);

Author Comment

ID: 24026069
I need a logic to convert an integer to packed decimal format. please follow the url '' for packed decimal. thanks
LVL 86

Expert Comment

ID: 24026499
You might try the Microfocus COBOL library or JRecord:
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 24027141
I do not have to work with cobol file, however I need to convert an integer defined in java to packed decimal.
LVL 15

Expert Comment

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

Expert Comment

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

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

Expert Comment

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


Accepted Solution

rishirajm earned 0 total points
ID: 24046031
Thanks CEHJ for your quick response. I was able to find solution at
LVL 86

Expert Comment

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web application structure 18 111
Java string replace 11 61
Glassfish admin console not working 1 56
java mysql insert application 14 41
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

735 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