Solved

How to show + sign in scientific notation.

Posted on 2004-09-05
4
653 Views
Last Modified: 2008-02-01
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
Comment
Question by:Emilda
  • 2
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
gnoon earned 50 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

by:Emilda
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

by:gnoon
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

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

Thanks for helping.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 62
tomcat not starting 6 45
login jsp example 24 46
Adding multiple JVM environments to RedHat 6 7 17
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…
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:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

816 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

12 Experts available now in Live!

Get 1:1 Help Now