Results 1 to 6 of 6
  1. #1
    JayP is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default Substring double values?

    I'm trying to substring a double value but I tried .substring but i believe that's only for strings.

    And I'd also like to round the numbers up a tiny bit.

    for example

    4.079691454585337

    rounded up to 4.08 without showing the rest of the numbers.

    Could anyone put me in the right direction for this. Thanks a bunch!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You can do this any number of ways, but first of all, you are right, subString is for Strings only. Now as to your problem, it can be solved with a DecimalFormat object or the static String method String.format(...).

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    I tried .substring but i believe that's only for strings.
    You can see what methods a class has by reading the API doc for that class.

    If you are printing the numbers and want to control what is printed, see the printf() method.

  4. #4
    JayP is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default

    I'm not sure what I'm doing wrong. I've checked the printf class and followed a tutorial but I get a problem with the script.

    Here's my code:

    Java Code:
    System.out.printf("%.1f \t %f", year, price);
    Is there a reason why there comes with an error with this? I can't see why, I've followed the tutorial and it seems correct.

    Any pointers would be greatly appreciated!

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Is there a reason why there comes with an error with this
    You forgot to post the full text of the error message.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Crystal ball says either year or price is not a floating point number. Which leads me to ask why is year a floating point number? This year is 2011 not 2011.56.

Similar Threads

  1. find the unique values of a double array
    By tyang in forum New To Java
    Replies: 3
    Last Post: 09-11-2011, 02:47 PM
  2. Double with no negative values.
    By vahini in forum New To Java
    Replies: 11
    Last Post: 05-05-2011, 09:07 AM
  3. Removing the double values?
    By Lund01 in forum Advanced Java
    Replies: 13
    Last Post: 11-17-2010, 11:34 AM
  4. Pick up a double quoted substring
    By java_user in forum New To Java
    Replies: 1
    Last Post: 01-08-2010, 05:12 PM
  5. mutliplicatio of double values
    By katkamravi in forum New To Java
    Replies: 2
    Last Post: 04-13-2009, 02:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •