From: abc on
rossum wrote:
> 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?

Aren't int and long both integer types though?
Wouldn't this cause the decimal fraction be lost?
From: markspace on
abc wrote:
> rossum wrote:
>> 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?
>
> Aren't int and long both integer types though?
> Wouldn't this cause the decimal fraction be lost?


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.

Sorry but I don't see a solution to your problem. I think you will have
to hack it. Regex looks good to me.



From: Eric Sosman on
On 12/21/2009 4:49 PM, abc wrote:
> Eric Sosman wrote:
>>> ....
>>> String s = Double.toString(x);
>>> s = s.replaceAll((String)"\\.0$", "");
>>
>> Perhaps it shows my age, but something inside me cringes
>> at the notion of wheeling out the regular expression cannon to
>> kill so simple a canary.
>
> Same here, hence my question.
>>
>> s = s.substring(s.indexOf('.'));
>
> Wouldn't this get rid of the decimal fraction part completely, whether
> it's non-zero or not? That's not what I want.

Actually, I botched the substring() call, and ought
to have written

s = s.substring(0, s.indexOf('.'));

.... and yes, this version would snip the decimal point and
everything that follows. That's what I thought you wanted,
but on re-reading your message I see that you only wanted to
jettison a literal ".0" and keep other fractional parts.
(I've obviously been hitting the eggnog too hard today ...).

In penance, here's another possibility:

if (s.endsWith(".0"))
s = s.substring(s.length() - 2);

>> "Cleaner" -- and certainly more flexible -- alternatives are
>> to use a java.text.DecimalFormat or a java.util.Formatter instead
>> of toString().
>
> Thanks, I'll read up on those.

Probably the best course, since they'll also spare you
from unpleasantnesses like "10.333333333333334". Note that
PrintStream has convenience methods for using Formatter.

--
Eric Sosman
esosman(a)ieee-dot-org.invalid
From: Jeff Higgins on
markspace wrote:
> abc wrote:
>> rossum wrote:
>>> 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?
>>
>> Aren't int and long both integer types though?
>> Wouldn't this cause the decimal fraction be lost?
>
>
> 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.
>
> Sorry but I don't see a solution to your problem. I think you will have
> to hack it. Regex looks good to me.
>
>
>


I don't either that why I replied as I did.

The .0 is acting as an indicator that the represented number is a
floating-point approximation, removing it hides that indication.

Good, bad, dirty, clean, pretty, plain?
Depends upon agreement between producer, consumer.
From: Jeff Higgins on
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$", "")