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

x
?
Solved

How to show + sign in scientific notation.

Posted on 2004-09-05
4
Medium Priority
?
687 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
[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
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 16

Accepted Solution

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

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

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!

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

618 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