Prev: Clean way to write Double into String without the trailing ".0" ?
Next: 32-bit characters in Java string literals
From: Knute Johnson on 21 Dec 2009 21:11 On 12/21/2009 2:17 PM, markspace wrote: > > Yes. Just to be clear to everyone, he wants to remove the trailing zeros > only if they are all zero. If he has a component after the decimal place > with digits besides 0, he wants it printed. > OK, then I like this :-). public class test { public static void main(String[] args) throws Exception { Double d = new Double(666); String s = d.toString(); System.out.println(s); if (s.endsWith(".0")) s = s.substring(0,s.length()-2); System.out.println(s); } } -- 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
From: Jeff Higgins on 21 Dec 2009 21:29 Jeff Higgins wrote: > Tom McGlynn wrote: > >> If you have very big numbers then you need to worry about exponent >> notation too. The real defect here is that it > Good catch. > OK Double.valueOf(1000000.0).toString().replaceAll("\\.0$", "") > OOPS Double.valueOf(10000000.0).toString().replaceAll("\\.0$", "") oops - Double has a static toString(double d) Double.toString(10000000.0d).replaceAll("\\.0$", "") <http://java.sun.com/javase/6/docs/api/java/lang/Double.html#toString(double)> <http://java.sun.com/javase/6/docs/api/java/lang/Double.html#valueOf(java.lang.String)>
From: Lew on 22 Dec 2009 00:57 Kevin McMurtrie wrote: > private static final NumberFormat s_fmt= > new DecimalFormat("0.################"); > > ... > final String str; > synchronized (s_fmt) > { > str= s_fmt.format(d); > } > ... I thought so, too, but that pattern doesn't eliminate the decimal point when the fractional part is zero. -- Lew
From: abc on 22 Dec 2009 03:27 Tom McGlynn wrote: > Personally I think what you've done (absent the redundant cast that > others have mentioned) is a perfectly fine way to do what you want. OK, thanks. There has been a couple of mentions of the "redundant cast". If what you mean by this is the (String)"\\.0$" I had to put it in because I found I get an error message there if I remove the (String) part like this: s = s.replaceAll("\\.0$", ""); // error, with (String) removed
From: Arved Sandstrom on 22 Dec 2009 06:14 Kevin McMurtrie wrote: > In article <hgpn3u$upc$1(a)news.albasani.net>, Lew <noone(a)lewscanon.com> > wrote: > >> Kevin McMurtrie wrote: >>> private static final NumberFormat s_fmt= >>> new DecimalFormat("0.################"); >>> >>> ... >>> final String str; >>> synchronized (s_fmt) >>> { >>> str= s_fmt.format(d); >>> } >>> ... >> I thought so, too, but that pattern doesn't eliminate the decimal point when >> the fractional part is zero. > > When I run it, 10.0d returns "10" and 10.1d returns "10.1". This is the one part of the original problem statement that may have to be rethought. Does the OP really want that decimal point to be dropped if the fractional part is zero? There is, after all, a big difference between the numbers "10" and "10." AHS
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: Clean way to write Double into String without the trailing ".0" ? Next: 32-bit characters in Java string literals |