Need help with Java( on the double )

Get help on programming - C++, Java, Delphi, etc.
Post Reply
stewy_w
Registered User
Posts: 486
Joined: 25 Sep 2003, 02:00
Location: Citta Del Cappo
Contact:

Need help with Java( on the double )

Post by stewy_w »

Anyone, plz help me asap

how do I round off to two decimal places in java(the datatype is double).
I tried using DecimalFormat but i get errors.
i am stuck because of this :cry:
please help
Running Vista is like being in a candy store that only sells black liquorice at exorbitant prices. Oh, and you\'re only allowed to eat the candy in the store.
ckritntt
Registered User
Posts: 28
Joined: 04 Jun 2004, 02:00

Post by ckritntt »

EG.
double d = 3.46434;

DecimalFormat twoDigits = new DecimalFormat();
double f = 0.00;
f = twoDigits.Format(d);

But make sure that you apply this mechanism so that all your double variables for you crafter's,product's and crafter product's look the same.
stewy_w
Registered User
Posts: 486
Joined: 25 Sep 2003, 02:00
Location: Citta Del Cappo
Contact:

Post by stewy_w »

ckritntt wrote: f = twoDigits.Format(d);
what's the d for?
Running Vista is like being in a candy store that only sells black liquorice at exorbitant prices. Oh, and you\'re only allowed to eat the candy in the store.
wit_skapie
Moderator Emeritus
Posts: 6866
Joined: 12 Dec 2003, 02:00
Location: JHB
Contact:

Post by wit_skapie »

Eish .... it's the number you want to format... check the example : double d = 3.46434;
Nerevar
Registered User
Posts: 3621
Joined: 21 Jul 2004, 02:00
Location: Stellenbosch
Contact:

Post by Nerevar »

or use

lets say

c.print(num1);
c.print(0,2);
Image
E6600@2.9GHz, Asus8800 GTS, 2x2GB
Patriot DDR2 4-4-4-12 @ 900MHz
RuadRauFlessa
Registered User
Posts: 20576
Joined: 19 Sep 2003, 02:00
Location: Bloodbank

Post by RuadRauFlessa »

Take note that the print and printf functions are only available in JDK5. The formatting within these two methods occurs exactly the same as in c's cprintf and cprint methods.

Anyway I would be more interested in seing the code that causes your problem and then trying to correct that than giving you a couple of examples.
:rock: :rock: :rock: :rock: :rock: :rock: :rock: :rock: :rock: :rock:
Spoiler (show)
Intel Core i7-2600k @ 3.4GHz
Corsair Vengence 2x4GB DDR3 2000MHz
Thermaltake Toughpower 850W
ASUS nVidia GTX560 1GB
CoolerMaster HAF 932
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Post by rustypup »

RuadRauFlessa wrote:Anyway I would be more interested in seing the code that causes your problem and then trying to correct that than giving you a couple of examples.
Agreed....

Problem : The alg used to handle rounding for doubles is prone to NaN or Infinity on certain cpu's.... ignore this at your own peril. You can check for these using the static methods Double.isNaN(dble) or Double.isInfinity(dble)....

Consider using BigDecimal or BigInteger in double precision ops....
RuadRauFlessa
Registered User
Posts: 20576
Joined: 19 Sep 2003, 02:00
Location: Bloodbank

Post by RuadRauFlessa »

Ello Rustypup. I weould like to welome you to the forums on behalf of all the moderators over here. Hope you have a plesent stay mate :D
:rock: :rock: :rock: :rock: :rock: :rock: :rock: :rock: :rock: :rock:
Spoiler (show)
Intel Core i7-2600k @ 3.4GHz
Corsair Vengence 2x4GB DDR3 2000MHz
Thermaltake Toughpower 850W
ASUS nVidia GTX560 1GB
CoolerMaster HAF 932
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Post by rustypup »

RuadRauFlessa wrote:Ello Rustypup. I weould like to welome you to the forums on behalf of all the moderators over here. Hope you have a plesent stay mate :D
I'm sure I will.... spend most days at jtech..... thought I'd give the locals a chance to make me look blonder than I already am... :)
RuadRauFlessa
Registered User
Posts: 20576
Joined: 19 Sep 2003, 02:00
Location: Bloodbank

Post by RuadRauFlessa »

We are all mates here, We laugth,play and shout together. :lol: Keep the source flowing man I gotta get back to work.
:rock: :rock: :rock: :rock: :rock: :rock: :rock: :rock: :rock: :rock:
Spoiler (show)
Intel Core i7-2600k @ 3.4GHz
Corsair Vengence 2x4GB DDR3 2000MHz
Thermaltake Toughpower 850W
ASUS nVidia GTX560 1GB
CoolerMaster HAF 932
Post Reply