-1

I want to round my number if it exceeds 8 character long.

For example,

// Big number rounding using scientific notation
double myDouble1 = 123456789;// desired output: 1.23e+08

Another situation

// Rounding
double myDouble2 = 12345.5678901234; // Desired output: 12345.57

I've tried using String.format() with %.2g and %.7, but I couldn't achieve the desired output.

Here's the code that I've tried to come up with.

 public String parseResult(String val){
    String formatted = val;
    try{
        if(formatted.length() > 8){
            double temp = Double.parseDouble(val);
            if(temp % 1 == 0){
                formatted = String.format("%.2g", temp);
            }else{
                formatted = String.format("%.7g", temp);
            }
        }
    }
    catch(NumberFormatException e)
    {
    }
    return formatted;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
root
  • 1,573
  • 3
  • 23
  • 38
  • possible duplicate of [How to round a number to n decimal places in Java](http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java) – Sergei Tachenov May 04 '15 at 04:33

2 Answers2

1
public class SolutionMain
{
    public static void main(String[] args)
    {
        double myDouble1 = 123456789; // Desired output: 1.23e+08
        double myDouble2 = 12345.5678901234; // Desired output: 12345.57

        System.out.println(parseResult(myDouble1));
        System.out.println(parseResult(myDouble2));
    }

    public static String parseResult(Double myDouble)
    {
        DecimalFormat format = null;

        if(myDouble.toString().length() > 8)
        {
            if(myDouble % 1 == 0)
                format = new DecimalFormat("0.00E00");
            else
                format = new DecimalFormat("#.00");
        }

        return format.format(myDouble);
    }
}

For more pattern format details: Customizing Formats

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Marie Diana Tran
  • 573
  • 6
  • 10
  • You can made your own format rules using DecimalFormatSymbols class. I suggest you to take a look at the Altering the Formatting Symbols section of [Customizing Formats](https://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html) – Marie Diana Tran May 04 '15 at 07:30
-1
class ScientificNot
{

      public static String getScientifiNotation(double n)
    { 
          int n1=(int)n;
          String s0=String.valueOf(n-(double)n1);
          String s1=String.valueOf((double)((int)n));
          int in=s1.indexOf(".");     
          String mantissa=null,exp=null;
          if(n>=10000000.0)
          {

             if(s1.length()>8)
             {
                  mantissa=s1.substring(0,3);
                  exp=s1.substring(3);
                  double man=Double.parseDouble(mantissa)/100.0;
                  return(man+"e"+exp.length());

                 }
             else
             return s1;
           }
          else if(s0.length()>8)
             {
               double num=(((double)((int)(n*1000)))); 
               int dp=((int)num%1000);
               if(dp%10>=5)
                      dp=(dp-(dp%10))+10;
               return String.valueOf(((int)num/1000)+"."+(dp/10));
               }
              else{
                 s1=""+n;

              }   
          return s1;
        }
    }