Re: Clean way to write Double into String without the trailing ".0"
?
On 12/21/2009 12:56 PM, abc wrote:
Easy newbie-question here for the experts:
What's a clean way of writing a Double into a String, avoiding
the ".0" at the end ?
The Double.toString() method insists on appending a decimal fraction
".0" no matter what, and I only want the fractions to show up if they
are non-zero.
I found a dirty workaround (see code below), using a regex to remove the
trailing ".0", but there has to be a prettier way to do this.
....
String s = Double.toString(x);
s = s.replaceAll((String)"\\.0$", "");
....
TIA
public class test {
public static void main(String[] args) throws Exception {
Double d = new Double(666.234);
System.out.println(String.format("%.0f",d.doubleValue()));
}
}
C:\Documents and Settings\Knute Johnson>java test
666
--
Knute Johnson
email s/nospam/knute2010/
--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access