How to convert a string 0E-11 to 0.00000000000 in Java? I want to display the number in non scientific notations. I've tried looking at the number formatter in Java, however I need to specific the exact number of decimals I want but I will not always know. I simply want the number of decimal places as specificed by my original number.
+2
A:
I would use BigDecimal.Pass your string into it as a parameter and then use String.format to represent your newly created BigDecimal without scientific notation. Float or Double classes can be used too.
eugener
2009-08-04 19:44:30
A:
I haven't tried it, but java.text.NumberFormat might do what you want.
R. Bemrose
2009-08-04 19:51:13
+1
A:
Apparently the correct answer is to user BigDecimal and retrieve the precision and scale numbers. Then use those numbers in the Formatter. Something similar like this:
BigDecimal bg = new BigDecimal(rs.getString(i));
Formatter fmt = new Formatter();
fmt.format("%." + bg.scale() + "f", bg);
buf.append( fmt);
erotsppa
2009-08-04 19:53:46
This is exactly what I suggested :)
eugener
2009-08-04 19:57:35
Yes, Eugene, but the devil is in the details; working example code is usually more helpful than a description. For example, someone suggested I use EXEC on a stored procedure (in Oracle 10g). The idea was correct (used a stored procedure instead of a function), but the details (CALL vs. EXEC) were wrong. As Torvalds once said, "Show me the code." http://lkml.org/lkml/2000/8/25/132
Dave Jarvis
2009-08-04 21:08:32
Are you joking? What kind of details do you need here? It is very simple already... the whole deal is about idea. :)
eugener
2009-08-04 21:22:54
A:
You don't need to know the exact number of places.
DecimalFormat df = new DecimalFormat(".######################");
String x = df.format(0.00001230000d);
// output = .0000123
Clint
2009-08-04 20:53:02