Solved

How to show + sign in scientific notation.

Posted on 2004-09-05
4
648 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

896 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

13 Experts available now in Live!

Get 1:1 Help Now