1. Member
Join Date
Jan 2010
Posts
8
Rep Power
0

## 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. 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.

3. Member
Join Date
Jan 2010
Posts
8
Rep Power
0
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. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18
Why do you need to truncate them like that?

5. Member
Join Date
Jan 2010
Posts
8
Rep Power
0

## 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. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18
They might mean BigDecimal. That allows you to faff with scales, which is what you want I think.

7. Senior Member
Join Date
Feb 2009
Posts
304
Rep Power
6
You could always do something like this...

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

8. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18
Is accuracy important? Just asking, because of the inevitable limitations of floating points in representing decimal fractions.

9. Member
Join Date
Jan 2010
Posts
8
Rep Power
0
i dont care about accuracy at all.

#### Posting Permissions

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