From: abc on

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
From: rossum on
On Mon, 21 Dec 2009 21:56:40 +0100, abc <abc(a)abc.net> 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 ?
Convert the double into an int or long first?

rossum

>
>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

From: Lew on

abc wrote:
>> 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$", "");
>

Jeff Higgins wrote:
> String s = Double.toString(x).replaceAll((String)"\\.0$", "");
>

Is there an echo in here?

Why the redundant cast of a 'String' to 'String'?

--
Lew

From: Tom McGlynn on
On Dec 21, 3:56 pm, abc <a...(a)abc.net> 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

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. I
think the code very clearly reflects your requirement.

If you are concerned with efficiency or feel that you don't want to
unleash the 'big guns' of regular expressions, then perhaps you could
put your conditional code before you do the conversion.
E.g. something like:

double MAX_TEST = Integer.MAX_VALUE;
...
String s;
int ix = (int) x; // If the (int) conversion is slow, perhaps
Math.floor(x)
// and only create the int when you need it.
if (Math.abs(x) <= MAX_TEST && (x == ix)) {
s = Integer.toString(ix);
} else {
s = Double.toString(x);
}

I suspect that's more efficient, but I'd check if it mattered. This
only handles numbers between -2^31 - 2^31. You can get bigger if you
use longs instead. If you have very big numbers then you need to
worry about exponent notation too. The real defect here is that it
that compared to your original the intent is much less clear.

Regards,
Tom McGlynn


From: Kevin McMurtrie on
In article <hgone2$h0o$1(a)news.albasani.net>, abc <abc(a)abc.net> 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

private static final NumberFormat s_fmt=
new DecimalFormat("0.################");

....
final String str;
synchronized (s_fmt)
{
str= s_fmt.format(d);
}
....
--
I won't see Google Groups replies because I must filter them as spam