Results 1 to 9 of 9
Thread: Working with float
- 01-26-2010, 10:08 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 8
- Rep Power
- 0
- 01-26-2010, 10:16 PM #2
System.out.printf(); or System.out.format(); works great
Output:Java Code:double x = 4.215312531235; System.out.printf("%.3f",x);
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 10: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)
- 01-27-2010, 06:58 AM #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.
- 01-27-2010, 09:29 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Why do you need to truncate them like that?
- 01-27-2010, 02:12 PM #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.
- 01-27-2010, 02:40 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
They might mean BigDecimal. That allows you to faff with scales, which is what you want I think.
- 01-27-2010, 02:58 PM #7
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
You could always do something like this...
Java Code:float fValue = 2.01656717611f; fValue = ((int)(fValue * 100)) / 100f;
- 01-27-2010, 03:11 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Is accuracy important? Just asking, because of the inevitable limitations of floating points in representing decimal fractions.
- 01-27-2010, 08:29 PM #9
Member
- Join Date
- Jan 2010
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
Float vs. Double
By javanub in forum New To JavaReplies: 1Last Post: 11-23-2008, 12:11 PM -
Put float value to double
By chamalsl in forum New To JavaReplies: 3Last Post: 09-20-2008, 04:52 AM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 AM -
Float to String
By mew in forum New To JavaReplies: 4Last Post: 12-29-2007, 05:08 PM -
Class float
By Peter in forum New To JavaReplies: 1Last Post: 07-08-2007, 01:17 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks