0

I am making an android application. The user will enter a 11-15 digit in android EditText as I have to do some mathematical work on this no, i need to convert it into double but so far this is not working well

how can i get the same string after parsing it into double

so far this is my code:

String Svalue="1101300045"
double Dvalue=Double.parseDouble(Svalue);
System.out.print("Dvalue: "+Dvalue);
Dvalue++; 
Svalue=Double.toString(Dvalue);
System.out.print("Svalue: "+Svalue)

OUTPUT:

Dvalue: 1.11013001045.E15 
Svalue: 1.11013001046.E15

DESIRED OUTPUT:

Dvalue: 1101300045
Svalue: 1101300046
51j0
  • 51
  • 1
  • 10

4 Answers4

1

To format a Double you make use of the NumberFormat utility class.

    String Svalue = "1101300045";
    double Dvalue = Double.parseDouble(Svalue);

    NumberFormat nf = NumberFormat.getInstance();
    nf.setGroupingUsed(false);
    System.out.println("Dvalue: " + nf.format(Dvalue));

    Dvalue++;
    Svalue = nf.format(Dvalue);
    System.out.println("Svalue: " + Svalue);

You can also format decimal values with rounding support like

    Svalue = "1101300045.98765";
    Dvalue = Double.parseDouble(Svalue);

    nf.setMaximumFractionDigits(2);
    System.out.println("Dvalue: " + nf.format(Dvalue));

Output :

Dvalue: 1101300045
Svalue: 1101300046
Dvalue: 1101300045.99
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
0

Try this one double Dvalue = Double.parseDouble(name.getText().toString()); System.out.print("Dvalue: "+Dvalue );

Svalue=Double.toString(Dvalue); System.out.print("Svalue: "+Svalue)

sanket
  • 21
  • 2
  • int Ivalue = Integer.parseInt(name.getText().toString()); System.out.print("Ivalue: "+Ivalue ); String Svalue=Integer.toString(Ivalue); System.out.print("Svalue:"+Svalue); you can use calculation on integer. The double data type is a double-precision 64-bit IEEE 754 floating point when you try to use String as 12.2345768 and then convert into double then it show you the correct value – sanket Feb 27 '15 at 09:38
  • noo int wont parse it ill be gone under exception.. please checck your answer before posting them – 51j0 Feb 27 '15 at 09:57
  • it is working fine String S="1101300045"; int i=Integer.parseInt(S); please check carefully System.out.print(i); – sanket Feb 27 '15 at 10:06
  • my dear, my number is too large to fit in an int which is 32 bits and only has a range of -2,147,483,648 to 2,147,483,647.. i am not that dumb .. in my questio i have mentioned that my String will contain 11-15 char – 51j0 Feb 27 '15 at 10:11
  • i know this thing but i send the above code after successfully executed just try to run above code on http://www.tutorialspoint.com/compile_java_online.php then check it – sanket Feb 27 '15 at 10:17
  • as per your request i executed my code in your site " and it returned me with numberexccetopn error".. – 51j0 Feb 27 '15 at 10:24
0

This question is answered several times. This answer provides several useful approaches.

Community
  • 1
  • 1
JHH
  • 8,567
  • 8
  • 47
  • 91
0

Method 1: Using Double.parseDouble

public static double parseDouble(String str) throws NumberFormatException It returns the double value represented by the string argument and throws following exceptions:

NullPointerException – if the specified String str is null.

NumberFormatException – if the string format is not valid. For e.g. If the string is 122.20ab this exception will be thrown.

String str="122.202";
Double var= Double.parseDouble(str);

Double variable var value would be 122.202 after conversion.

Method 2: Using Double.valueOf

String str3="122.202";
Double var3= Double.valueOf(str3);

The value of var3 would be 122.202.

Method 3: Double class constructor

String str2="122.202";
Double var2= new Double(str2);

Double class has a constructor which parses the passed String argument and returns a double value.

public Double(String s) throws NumberFormatException

Constructs a newly allocated Double object that represents the floating-point value of type double represented by the string.

Complete Example:

try{
   //Using parseDouble
   String str="122.202";
   Double var= Double.parseDouble(str);
   System.out.println(var);

   String str2="122.202";
   Double var2= new Double(str2);
   System.out.println(var2);

   //Using valueOf method
   String str3="122.202";
   Double var3= Double.valueOf(str3);
   System.out.println(var3);

}catch (NullPointerException e){ e.printStackTrace();}
 catch (NumberFormatException e){ e.printStackTrace();}
}

Output:

122.202
122.202
122.202

As additional, here's how to get the text from EditText:

EditText et = (EditText) findViewById(R.id.myEditText);
String text = et.getText().toString();

Source.

Anggrayudi H
  • 14,977
  • 11
  • 54
  • 87
  • Whatever the content of your `String`, you are free to enter any value, whether it's a decimal value or not. So, just put the value you want into `EditText` (i.e. `1101300045`). – Anggrayudi H Feb 27 '15 at 09:17
  • it wont work,in my answer i have already showed you the output of this scenario – 51j0 Feb 27 '15 at 09:59