Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Clean way to write Double into String without the trailing ".0" ?

Reply
Thread Tools

Clean way to write Double into String without the trailing ".0" ?

 
 
abc
Guest
Posts: n/a
 
      12-21-2009

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
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      12-21-2009
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.doubleVa lue()));
}
}

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
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      12-21-2009
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
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Jeff Higgins
Guest
Posts: n/a
 
      12-21-2009
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$", "");
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      12-21-2009

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

 
Reply With Quote
 
Jeff Higgins
Guest
Posts: n/a
 
      12-21-2009
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
 
Reply With Quote
 
abc
Guest
Posts: n/a
 
      12-21-2009
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.
 
Reply With Quote
 
abc
Guest
Posts: n/a
 
      12-21-2009
rossum wrote:
> On Mon, 21 Dec 2009 21:56:40 +0100, abc <(E-Mail Removed)> 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?
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      12-21-2009
abc wrote:
> rossum wrote:
>> On Mon, 21 Dec 2009 21:56:40 +0100, abc <(E-Mail Removed)> 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.



 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      12-21-2009
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
(E-Mail Removed)lid
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
replace double inverted commas into slash double inverted commas in string AviraM Java 2 09-28-2006 06:19 PM
Proper way to write a CLEAN xml file (erasing old data first) darrel ASP .Net 4 05-05-2006 06:57 PM
Convert float to string without trailing 0s Mike C Programming 9 04-18-2005 05:31 PM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
Printing double with no trailing zeros Michael C Programming 3 11-14-2003 04:50 AM



Advertisments