Convert double to String - with only 3 decimal places

Discussion in 'Java' started by boris.mueller@gmail.com, Jul 20, 2005.

  1. Guest

    Is there an easy way to convert a double to a String and specify the
    number of decimal places? Let's say I only need 3 decimal places, so
    the double 1.2345678 should be 1.234 as a String or 999.123456 should
    be 999.123.

    In Python I can do it very elegantly like this:
    shortString = "%.3f" % myDouble

    I am currently using the following code, but I think it is quite
    dodgy... (And it ignores the scientific notation.)

    public String strPre(double inValue){
    String shortString;
    if (inValue < 0.001){
    shortString ="0";
    }
    else{
    String doubleString = Double.toString(inValue);
    String [] stringArray = doubleString.split("\\.");
    String decimals = stringArray[1].substring(0,3);
    shortString = stringArray[0] + "." + decimals;
    }
    return shortString;
    }

    Any help would be appreciated!
     
    , Jul 20, 2005
    #1
    1. Advertising

  2. On 20 Jul 2005 12:28:22 -0700, wrote:

    > Let's say I only need 3 decimal places..


    OK, "you only need 3 decimal places". Do it at output..
    <http://java.sun.com/j2se/1.5.0/docs/api/java/text/NumberFormat.html>
    <http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html>

    HTH

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    Too Hot For Radio
     
    Andrew Thompson, Jul 20, 2005
    #2
    1. Advertising

  3. Eric Sosman Guest

    Andrew Thompson wrote:
    > On 20 Jul 2005 12:28:22 -0700, wrote:
    >
    >
    >>Let's say I only need 3 decimal places..

    >
    >
    > OK, "you only need 3 decimal places". Do it at output..
    > <http://java.sun.com/j2se/1.5.0/docs/api/java/text/NumberFormat.html>
    > <http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html>


    NumberFormat will do what boris.mueller probably
    wants, but I don't think it will do what he asked for.

    --
     
    Eric Sosman, Jul 20, 2005
    #3
  4. On Wed, 20 Jul 2005 16:07:16 -0400, Eric Sosman wrote:

    > Andrew Thompson wrote:
    >> On 20 Jul 2005 12:28:22 -0700, wrote:
    >>
    >>>Let's say I only need 3 decimal places..

    >>
    >> OK, "you only need 3 decimal places". Do it at output..
    >> <http://java.sun.com/j2se/1.5.0/docs/api/java/text/NumberFormat.html>
    >> <http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html>

    >
    > NumberFormat will do what boris.mueller probably
    > wants, but I don't think it will do what he asked for.


    "Don't give them what they ask for, give them what they need"
    ...Can you tell why I don't work in a service industry? ;-)

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    Too Hot For Radio
     
    Andrew Thompson, Jul 20, 2005
    #4
  5. Guest

    OK, I followed Andrews hints and got this far:

    import java.text.DecimalFormat;
    public String strPre(double inValue){
    DecimalFormat threeDec = new DecimalFormat("0.000");
    String shortString = (threeDec.format(inValue));
    return shortString;
    }

    This basically works, but I am getting a comma as a seperator - not a
    dot. I assume that's because I am working on a german system... Any
    ideas?
     
    , Jul 20, 2005
    #5
  6. Guest

    I have added the Locale and I am now getting the desired result. Still
    seems to be a bit of a hack...

    public String strPre(double inValue){
    String shortString = "";
    DecimalFormat threeDec = new DecimalFormat("0.000", new
    DecimalFormatSymbols(Locale.US));
    shortString = (threeDec.format(inValue));
    return shortString;
    }

    Anyway, thanks for the help.
     
    , Jul 20, 2005
    #6
  7. wrote:
    > Is there an easy way to convert a double to a String and specify the
    > number of decimal places? Let's say I only need 3 decimal places, so
    > the double 1.2345678 should be 1.234 as a String or 999.123456 should
    > be 999.123.
    >
    > In Python I can do it very elegantly like this:
    > shortString = "%.3f" % myDouble
    >
    > I am currently using the following code, but I think it is quite
    > dodgy... (And it ignores the scientific notation.)
    >
    > public String strPre(double inValue){
    > String shortString;
    > if (inValue < 0.001){
    > shortString ="0";
    > }
    > else{
    > String doubleString = Double.toString(inValue);
    > String [] stringArray = doubleString.split("\\.");
    > String decimals = stringArray[1].substring(0,3);
    > shortString = stringArray[0] + "." + decimals;
    > }
    > return shortString;
    > }
    >
    > Any help would be appreciated!
    >


    What's wrong with this?
    public String strPre(double inValue) {
    return Double.toString(((int)(inValue * 1000))/1000.0);
    }

    You would have to pad trailing zeros if you always needed three, but it
    gives you the precision you want in the String.

    You could also use this, or a modified version, to ensure padding and
    use formatting codes (if you're more comfortable with them and can use
    Java 5 features):
    public String strPre(double inValue) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    pw.printf("%.3f", inValue);
    return sw.toString();
    }
    --Paul
     
    Paul Bilnoski, Jul 20, 2005
    #7
  8. john Guest

    Wed, 20 Jul 2005 13:54:23 -0700, boris.mueller wrote:

    > I have added the Locale and I am now getting the desired result. Still
    > seems to be a bit of a hack...
    >
    > public String strPre(double inValue){
    > String shortString = "";
    > DecimalFormat threeDec = new DecimalFormat("0.000", new
    > DecimalFormatSymbols(Locale.US));
    > shortString = (threeDec.format(inValue));
    > return shortString;
    > }
    >
    > Anyway, thanks for the help.



    Try the following. A little cleaner without the need for the locale, and
    will eliminate the groupings.

    import java.text.DecimalFormat;

    public String strPre(double inValue){
    DecimalFormat threeDec = new DecimalFormat("0.000");
    threeDec.setGroupingUsed(false);
    return threeDec.format(inValue);
    }

    Regards,
    John
     
    john, Jul 21, 2005
    #8
  9. Alan Krueger Guest

    Paul Bilnoski wrote:
    > What's wrong with this?
    > public String strPre(double inValue) {
    > return Double.toString(((int)(inValue * 1000))/1000.0);
    > }


    For whatever reason, for all values I've cared to test in the range

    0.001 <= i < 0.01

    this function returns "0.00X0", where X is the digit that remains after
    the transform. (JDK 1.5.0_03 on Windows XP)
     
    Alan Krueger, Jul 21, 2005
    #9
  10. boris Guest

    > DecimalFormat threeDec = new DecimalFormat("0.000");

    If I leave out the locale, I am stuck again with the comma. (0,123
    instead of 0.123.)
     
    boris, Jul 21, 2005
    #10
  11. Alan Krueger Guest

    john wrote:
    > Try the following. A little cleaner without the need for the locale, and
    > will eliminate the groupings.


    Groupings are the non-significant separators at thousands, millions,
    etc. places. This is not to what he was referring. In many (most?)
    European locales, the decimal separator is a comma not a dot, and the
    grouping separator is a dot not a comma.

    He's seeing a different decimal separator than what he wants, and needs
    the specific locale to get what he wants.
     
    Alan Krueger, Jul 21, 2005
    #11
  12. Alan Krueger Guest

    wrote:
    > I have added the Locale and I am now getting the desired result. Still
    > seems to be a bit of a hack...
    >
    > public String strPre(double inValue){
    > String shortString = "";
    > DecimalFormat threeDec = new DecimalFormat("0.000", new
    > DecimalFormatSymbols(Locale.US));
    > shortString = (threeDec.format(inValue));
    > return shortString;
    > }


    You could shorten it a little by eliminating the shortString variable
    and returning the result of threeDec.format(inValue) directly.
     
    Alan Krueger, Jul 21, 2005
    #12
  13. Wayne Guest

    wrote:
    > Is there an easy way to convert a double to a String and specify the
    > number of decimal places? Let's say I only need 3 decimal places, so
    > the double 1.2345678 should be 1.234 as a String or 999.123456 should
    > be 999.123.
    >
    > In Python I can do it very elegantly like this:
    > shortString = "%.3f" % myDouble
    >
    > I am currently using the following code, but I think it is quite
    > dodgy... (And it ignores the scientific notation.)
    >
    > public String strPre(double inValue){
    > String shortString;
    > if (inValue < 0.001){
    > shortString ="0";
    > }
    > else{
    > String doubleString = Double.toString(inValue);
    > String [] stringArray = doubleString.split("\\.");
    > String decimals = stringArray[1].substring(0,3);
    > shortString = stringArray[0] + "." + decimals;
    > }
    > return shortString;
    > }
    >
    > Any help would be appreciated!
    >


    I don't know why no one pointed this out yet, but Java5 has added
    printf fuctions. You can do what you with much like with
    Python (or C or Perl or ...):

    String s = String.format( "%.3f", myDouble );

    or:

    String s = String.format( myLocale, "%.3f", myDouble );

    -Wayne
     
    Wayne, Jul 21, 2005
    #13
  14. On Thu, 21 Jul 2005 16:41:43 GMT, Wayne wrote:

    > I don't know why no one pointed this out yet,


    Paul Bilnoski..

    >..but Java5 has added printf fuctions.


    ...<http://groups.google.com.au/group/comp.lang.java.programmer/msg/1362c4ebbd006026>

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    Featuring Gratuitous Alien Nudity
     
    Andrew Thompson, Jul 21, 2005
    #14
  15. rkjessop

    Joined:
    Jul 6, 2012
    Messages:
    1
    This response is for everyone else that gets to this site years later:
    String.format("%.3f", <double>)
     
    rkjessop, Jul 6, 2012
    #15
    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. Sydex
    Replies:
    12
    Views:
    6,653
    Victor Bazarov
    Feb 17, 2005
  2. pvong
    Replies:
    2
    Views:
    2,202
  3. FAQ server
    Replies:
    1
    Views:
    160
    Bart Van der Donck
    Aug 19, 2006
  4. FAQ server
    Replies:
    6
    Views:
    182
    Dr J R Stockton
    Nov 3, 2006
  5. FAQ server
    Replies:
    52
    Views:
    560
Loading...

Share This Page