Rounding a double to 2 decimal points in java...how?  | | |
October 7th, 2002, 02:42 AM
|
#1 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Pasadena, CA
Posts: 2,177
| Rounding a double to 2 decimal points in java...how?
Im trying to find the method that will round a double number to 2 decimal places for showing a final value of money. I've been looking through books and Im pretty sure its in the .math class but I cant find a simple method that will do it (its not supposed to be that advanced of a program). Any help will be great.
__________________
YAH! I knew you'd be jealous
|
| |
October 7th, 2002, 02:46 AM
|
#2 (permalink)
| | ph34r t3h g04t
Join Date: Oct 2001 Location: Kingsford, MI
Posts: 19,577
|
Heh, write your own little dealy? Turn it into a string, use the . as a delimiter, grab the third digit past it, integer it, compare it, str cat some stuff. Heck ya man, no problem!!
<- VB, no Java here.  |
| |
October 7th, 2002, 04:25 AM
|
#3 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Pasadena, CA
Posts: 2,177
|
Wow Whir great!!! Thats an easy fix to an easy problem  hehe....darn you VB peepz!!! |
| |
October 7th, 2002, 04:34 AM
|
#4 (permalink)
| | ph34r t3h g04t
Join Date: Oct 2001 Location: Kingsford, MI
Posts: 19,577
|
Well, I assume you can do all the same stuff in Java, I just don't know the proper functions. Java and VB are THAT much different.  |
| |
October 7th, 2002, 06:12 AM
|
#5 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Pasadena, CA
Posts: 2,177
|
hehe...indeed
Soooo...now that Whir and I have played  anyone have an answer? |
| |
October 7th, 2002, 08:12 PM
|
#6 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 1,542
|
How about
double d, d2;
d2 = ((long) d * 100) / 100.0;  |
| |
October 7th, 2002, 08:15 PM
|
#7 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 1,542
|
Ooh, maybe this'd be better:
double d, d2;
d2 = Math.round(d * 100) / 100.0
I think the first floors (or maybe truncates) the number-to-integer conversion
Course you can use Math.floor(), Math.ceil() etc as you see fit  |
| |
October 7th, 2002, 08:23 PM
|
#8 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 1,542
|
Sorry, should have actually read the question 
import java.text.NumberFormat;
double d;
String s;
NumberFormat nf;
nf = NumberFormat.getCurrencyInstance();
s = nf.format(d); |
| |
October 7th, 2002, 08:32 PM
|
#9 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 1,542
|
Last edited by strangerstill : October 7th, 2002 at 09:28 PM.
|
| |
October 7th, 2002, 09:11 PM
|
#10 (permalink)
| | Senior Member
Join Date: Oct 2001
Posts: 881
|
the last one is the best stranger still...
whir's method won't round the number correctly unless you add some extra methods... but that's what the api is for!
-Z |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |