Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

Posted on 2004-09-05
Medium Priority
687 Views
I want to convert an integer number into scientific number format with a plus sign after E.
e.g. I want to print 5 as .50000E+01 . Is this possible in Java. I have given below the code snippet I used in my program but with that it doesn't show a + sign. Can anybody help to solve this problem ?

int myNumber = 5;
String myFormat = ".00000E00";
NumberFormat nf = NumberFormat.getInstance();
DecimalFormat df = (DecimalFormat)nf;
df.applyPattern(myFormat);
System.out.println( "[" + myNumber +"]: " + df.format(myNumber) );
0
Question by:Emilda
[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
• 2
• 2

LVL 16

Accepted Solution

gnoon earned 200 total points
ID: 11988154
Try this

int myNumber = 5;
String myFormat = ".00000E00";
DecimalFormat df = new DecimalFormat(myFormat);
df.setPositivePrefix("+");
System.out.println( "[" + myNumber +"]: " + df.format(myNumber) );

It prints out:     [5]: +.50000E+01
0

Author Comment

ID: 11993790
Thanks, but it printed a + in the beginning as well. Is there any way to get rid of that.
0

LVL 16

Expert Comment

ID: 11994969

int myNumber = 5;
String myFormat = ".00000E00";
DecimalFormat df = new DecimalFormat(myFormat);
df.setPositivePrefix("+");

String notation = df.format(myNumber);
notation = notation.startsWith("+") ? notation.substring(1) : notation ;

System.out.println( "[" + myNumber +"]: " + notation );

0

Author Comment

ID: 12003997
:-), actually I was wondering whether there is any API available for formatting rather than a workaround.

Thanks for helping.
0

## Featured Post

Question has a verified solution.

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

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 will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.
###### Suggested Courses
Course of the Month11 days, 4 hours left to enroll