Solved

How to show + sign in scientific notation.

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

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

Suggested Solutions

Title # Comments Views Activity
Custom Java Application connects to database: Which one of the 20? 4 41
jsp error 6 46
Running JavaFX on the Raspberry Pi 27 136
IBM TS2900 (3572) Tape Autoloader Java? 12 61
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

733 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