Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Float

 
Mahesh Mamani
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I want to get float value of a string, but for small values I get it in the form "6.0E-4". Can any1 help me in getting 0.0006 in Float value???

public class flt1{
public static void main(String kk[]){
String val = "0.0006";
Float resultflt = new Float(val);
System.out.println("Flt val is "+resultflt.floatValue());
}
}
Thanks in advance.
Mahesh
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps use DecimalFormat:

Good Luck.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahesh, try using the code you already wrote. The new Float(String) constructor already understands how to parse "6.0E-04" and the like. Incidentally you can also use Float.parseFloat(String) to get a float directly, without using a Float. Enjoy...
 
Mahesh Mamani
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
10x Dirk,
It worked
Mahesh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic