Results 1 to 9 of 9
  1. #1
    dardar is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default Working with float

    i using a vector of floating points.
    when i print the vector the numbers looks like 2.2064104561

    how do i "chop" them to something like 2.206 or so?

    thank you

  2. #2
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    System.out.printf(); or System.out.format(); works great

    Java Code:
    double x = 4.215312531235;
    System.out.printf("%.3f",x);
    Output:
    Java Code:
    4.215

    EDIT: System.out.printf() or System.out.format() will round. If you want to floor it (meaning round down) , subtract 0.5 from the float var and then do the System.out.printf() or System.out.format().
    Last edited by Lil_Aziz1; 01-26-2010 at 11:45 PM.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  3. #3
    dardar is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default

    let me rephrase my problem.

    I'm using the line
    double ans = Math.random();

    ans looks like 2.01656717611 (ugly....)
    is there a way after I'm using the random method to "chop" the number to 2.01

    the printing is not the issue. i need to work with "relativity" large numbers.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Why do you need to truncate them like that?

  5. #5
    dardar is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default because!

    it's hard to explain why......it's part of an algirthm i'm using.

    someone told me the Class Double has a method to do that but i cant find it.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    They might mean BigDecimal. That allows you to faff with scales, which is what you want I think.

  7. #7
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    You could always do something like this...

    Java Code:
    float fValue = 2.01656717611f;
    fValue =     ((int)(fValue * 100)) / 100f;

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Is accuracy important? Just asking, because of the inevitable limitations of floating points in representing decimal fractions.

  9. #9
    dardar is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

Similar Threads

  1. Float vs. Double
    By javanub in forum New To Java
    Replies: 1
    Last Post: 11-23-2008, 01:11 PM
  2. Put float value to double
    By chamalsl in forum New To Java
    Replies: 3
    Last Post: 09-20-2008, 05:52 AM
  3. Replies: 8
    Last Post: 05-28-2008, 08:00 AM
  4. Float to String
    By mew in forum New To Java
    Replies: 4
    Last Post: 12-29-2007, 06:08 PM
  5. Class float
    By Peter in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 02:17 AM

Posting Permissions

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