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

Discussion in 'Java' started by abc, Dec 21, 2009.

  1. abc

    abc Guest

    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
     
    abc, Dec 21, 2009
    #1
    1. Advertising

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

    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
     
    Knute Johnson, Dec 21, 2009
    #2
    1. Advertising

  3. abc

    Eric Sosman Guest

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

    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
    lid
     
    Eric Sosman, Dec 21, 2009
    #3
  4. abc

    Jeff Higgins Guest

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

    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$", "");
     
    Jeff Higgins, Dec 21, 2009
    #4
  5. abc

    Lew Guest

    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
     
    Lew, Dec 21, 2009
    #5
  6. abc

    Jeff Higgins Guest

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

    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
     
    Jeff Higgins, Dec 21, 2009
    #6
  7. abc

    abc Guest

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

    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.
     
    abc, Dec 21, 2009
    #7
  8. abc

    abc Guest

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

    rossum wrote:
    > On Mon, 21 Dec 2009 21:56:40 +0100, 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 ?

    > 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?
     
    abc, Dec 21, 2009
    #8
  9. abc

    markspace Guest

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

    abc wrote:
    > rossum wrote:
    >> On Mon, 21 Dec 2009 21:56:40 +0100, 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 ?

    >> 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.
     
    markspace, Dec 21, 2009
    #9
  10. abc

    Eric Sosman Guest

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

    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
    lid
     
    Eric Sosman, Dec 21, 2009
    #10
  11. abc

    Jeff Higgins Guest

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

    markspace wrote:
    > abc wrote:
    >> rossum wrote:
    >>> On Mon, 21 Dec 2009 21:56:40 +0100, 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 ?
    >>> 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.
     
    Jeff Higgins, Dec 21, 2009
    #11
  12. abc

    Tom McGlynn Guest

    On Dec 21, 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$", "");
    > ....
    >
    > 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
     
    Tom McGlynn, Dec 22, 2009
    #12
  13. abc

    Jeff Higgins Guest

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

    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$", "")
     
    Jeff Higgins, Dec 22, 2009
    #13
  14. Re: Clean way to write Double into String without the trailing ".0"?

    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
     
    Knute Johnson, Dec 22, 2009
    #14
  15. abc

    Jeff Higgins Guest

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

    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)>
     
    Jeff Higgins, Dec 22, 2009
    #15
  16. In article <hgone2$h0o$>, 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


    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
     
    Kevin McMurtrie, Dec 22, 2009
    #16
  17. abc

    Lew Guest

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

    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
     
    Lew, Dec 22, 2009
    #17
  18. abc

    Lew Guest

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

    Kevin McMurtrie wrote:
    > private static final NumberFormat s_fmt=
    > new DecimalFormat("0.################");
    >
    > ...
    > final String str;
    > synchronized (s_fmt)
    > {
    > str= s_fmt.format(d);
    > }
    > ...


    In a heavily multi-threaded, high-throughput environment that synchronized
    static formatter could be quite a choke point. If that turns out to be the
    case, one could use

    str = new DecimalFormat("0.################").format(d);

    Naturally you'd want to elevate the string to a final constant either way.

    This assumes you don't mind keeping the decimal point when the fractional part
    is zero.

    Java has come a long way in optimization of uncontended locks, but contended
    ones are inherently a bitch. It can be astonishing how much time is spent
    waiting to acquire a lock in a contentious scenario, to the severe detriment
    of throughput. On projects where I've seen such things actually measured, a
    highly-contended synchronization lock has been the major throughput killer,
    beating even database I/O despite the latter being very poorly handled in some
    cases. And that was with Java 5.

    If the formatter is lightly touched, then the lock won't be such an issue.

    --
    Lew
     
    Lew, Dec 22, 2009
    #18
  19. In article <hgpn3u$upc$>, Lew <>
    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".
    --
    I won't see Google Groups replies because I must filter them as spam
     
    Kevin McMurtrie, Dec 22, 2009
    #19
  20. abc

    abc Guest

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

    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
     
    abc, Dec 22, 2009
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. darrel
    Replies:
    4
    Views:
    554
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    May 5, 2006
  2. Sydex
    Replies:
    12
    Views:
    6,500
    Victor Bazarov
    Feb 17, 2005
  3. AviraM
    Replies:
    2
    Views:
    6,374
    Manish Pandit
    Sep 28, 2006
  4. Mike
    Replies:
    9
    Views:
    2,246
    Martin Ambuhl
    Apr 18, 2005
  5. Replies:
    8
    Views:
    513
Loading...

Share This Page