Solved

# how to convert an integer to packed decimal in java

Posted on 2009-03-30
812 Views
can some one help to provide sample code for converting integer to packed decimal
0
Question by:rishirajm
[X]
###### 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
• 4
• 3
• 2

LVL 15

Expert Comment

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

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

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

0

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.

0

LVL 15

Expert Comment

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

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
0

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;
}
``````
0

Accepted Solution

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

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

## Featured Post

Question has a verified solution.

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

### Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Javaâ€¦
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Booleâ€¦
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.
###### Suggested Courses
Course of the Month8 days, 13 hours left to enroll