Solved

How to show + sign in scientific notation.

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

760 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

18 Experts available now in Live!

Get 1:1 Help Now