From: Knute Johnson on
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
From: Eric Sosman on
On 12/21/2009 3: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$", "");

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.

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

"Cleaner" -- and certainly more flexible -- alternatives are
to use a java.text.DecimalFormat or a java.util.Formatter instead
of toString().

--
Eric Sosman
esosman(a)ieee-dot-org.invalid
From: Jeff Higgins on
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
String s = Double.toString(x).replaceAll((String)"\\.0$", "");
From: Jeff Higgins on
Lew wrote:
> 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'?
>

OOPS .. Oops .. oops

negligent c/p
thanks
From: abc on
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.


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