home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

Rounding a double to 2 decimal points in java...how?

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 2377
Discussions: 200,993, Posts: 2,379,926, Members: 246,362
Old October 7th, 2002, 02:42 AM   Digg it!   #1 (permalink)
Ultimate Member
 
Tekk's Avatar
 
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
Tekk is offline   Reply With Quote
Old October 7th, 2002, 02:46 AM     #2 (permalink)
ph34r t3h g04t
 
Whir's Avatar
 
Join Date: Oct 2001
Location: Kingsford, MI
Posts: 19,577
Blog Entries: 7
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.
Whir is online now   Reply With Quote
Old October 7th, 2002, 04:25 AM     #3 (permalink)
Ultimate Member
 
Tekk's Avatar
 
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!!!
Tekk is offline   Reply With Quote
Old October 7th, 2002, 04:34 AM     #4 (permalink)
ph34r t3h g04t
 
Whir's Avatar
 
Join Date: Oct 2001
Location: Kingsford, MI
Posts: 19,577
Blog Entries: 7
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.
Whir is online now   Reply With Quote
Old October 7th, 2002, 06:12 AM     #5 (permalink)
Ultimate Member
 
Tekk's Avatar
 
Join Date: Oct 2001
Location: Pasadena, CA
Posts: 2,177
hehe...indeed

Soooo...now that Whir and I have played anyone have an answer?
Tekk is offline   Reply With Quote
Old October 7th, 2002, 08:12 PM     #6 (permalink)
Ultimate Member
 
strangerstill's Avatar
 
Join Date: Oct 2001
Posts: 1,542
How about
double d, d2;
d2 = ((long) d * 100) / 100.0;
strangerstill is offline   Reply With Quote
Old October 7th, 2002, 08:15 PM     #7 (permalink)
Ultimate Member
 
strangerstill's Avatar
 
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
strangerstill is offline   Reply With Quote
Old October 7th, 2002, 08:23 PM     #8 (permalink)
Ultimate Member
 
strangerstill's Avatar
 
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);
strangerstill is offline   Reply With Quote
Old October 7th, 2002, 08:32 PM     #9 (permalink)
Ultimate Member
 
strangerstill's Avatar
 
Join Date: Oct 2001
Posts: 1,542
Or say you wanna bit more control:

import java.text.DecimalFormat;
double d;
String s;
DecimalFormat df;
df = new DecimalFormat("¤0.00;(¤0.00)");
s = df.format(d);

http://java.sun.com/j2se/1.4.1/docs/...malFormat.html , natch

(href corrected, thanks zskillz)

Last edited by strangerstill : October 7th, 2002 at 09:28 PM.
strangerstill is offline   Reply With Quote
Old October 7th, 2002, 09:11 PM     #10 (permalink)
Senior Member
 
Join Date: Oct 2001
Posts: 881
Send a message via AIM to zskillz
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
zskillz is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (3088)
Foxconn Blackops x48 MoBo (5)
Charges against non-tippers dropped.. (21)
Health Care Rationing (16)
Delete an OS (17)
Nvidia GTX 260 problem (12)
Laptop with wireless problem. (12)
Wireless Televisions. (12)
windows vista security holes (19)
CPU fan stops spinning randomly (11)
Regular Build (11)
[F@H SPAM 11/16/09] ! 1/2 months to.. (41)
Point and Shoot Camera Suggestions. (8)
windows 7 problem (7)
Recent Discussions
Is the PSU I received dead? (16)
FreeAgent drive software not x64 comp.. (1)
Intel 5100 AGN issues fixed yet? (28)
Nvidia GTX 260 problem (12)
Foxconn Blackops x48 MoBo (5)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (41)
Print spooler problem (17)
Q9650 vs. Q9550 (2)
Desktop Calendar Application (2)
Looking for new motherboard (1)
soundmon.exe (8)
Jedi Academy Problem (3)
Can a page file be "too big".. (1)
Point and Shoot Camera Suggestions. (8)
Size after cutting 700Mb file is 2.5 .. (0)
Delete an OS (17)
windows vista security holes (19)
updating BIOS via winflash, claims fi.. (1)
New Server Configuration Suggestions (0)
cheap gaming laptop? (12)
Unallocated Space (2)
help me pls laptop just stopped worki.. (1)
C# + LINQ Help (7)
Dynex DX E-402 (3)
EVGA 9800 gtx help with finding a goo.. (12)


All times are GMT -4. The time now is 10:56 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28