
-----------------------------------
lionheart
Mon Jan 24, 2005 10:13 pm

formatting...
-----------------------------------
im kinda a noob at java but is it possible to format without outputting?
for example...
String mortStr = Double.toString (mortgage);
if a large number was inputted and calculations occured the string would have alot of decimals..
so im wondering if there is a way to format or
change it so that i around 2 decimal places?
Math.round works... but it would be easier for me if i could have 2 decimal places

-----------------------------------
Hikaru79
Mon Jan 24, 2005 10:17 pm


-----------------------------------
This is a neat trick. You want it to two decimal places? While it's still a double, multiply it by 100, round it, and divide it by 100. For example:

Your number is 64.56945
Multiply by 100 = 6456.945
Round to nearest whole number = 6457
Divide by 100 = 64.57
And voila! 64.57 is what you would normally have gotten from rounding to two. In java, this looks like


myDouble = 64.56945;
myDouble = (double) (Math.round (myDouble*100))/100;


That should work =) Remember, comp sci is all about creative problem solving ^_^

-----------------------------------
lionheart
Mon Jan 24, 2005 10:20 pm

Yea but...
-----------------------------------
see the thing is .... if it was a number lik 23232.23232323223 
the decimals are a result of  a division or multiplication of a decimal,
so then even if you multiply by 100 or divide...
then it will still have 2323223.232323
so i want to "get rid" of the extra decimals


actaully wait..

-----------------------------------
lionheart
Mon Jan 24, 2005 10:26 pm

yea that actually did...
-----------------------------------
that did work... thanks a  million hikaru
i ve spent so long on this damn final project....

-----------------------------------
Hikaru79
Mon Jan 24, 2005 10:27 pm


-----------------------------------
No problem, Lionheart =) Pass it on!  :D

-----------------------------------
lionheart
Mon Jan 24, 2005 10:46 pm

well is it..
-----------------------------------
what if its .... .say...
1234567.0
multiply..... its 
123456700
divide it still is  1234567.0
the reason it so impotant, is beause im trying to create a chart...

-----------------------------------
Neo
Mon Jan 24, 2005 10:53 pm

Re: well is it..
-----------------------------------
what if its .... .say...
1234567.0
multiply..... its 
123456700
divide it still is  1234567.0
the reason it so impotant, is beause im trying to create a chart...

You said you needed your numbers rounded to two decimal places, and since that number is already rounded, couldn't you just add another zero to the end of that number?

-----------------------------------
lionheart
Mon Jan 24, 2005 10:59 pm

but...
-----------------------------------
but if the number was something like...
1234567.192
and you added ... .12345670.192
then when you multitply round and divide..
you would get rid of the las number and aadd a uneeded 0

-----------------------------------
wtd
Tue Jan 25, 2005 12:23 am


-----------------------------------
A better solution would be to use a DecimalFormat object.

First you need to import java.text.

import java.text.*;

Then you create a DecimalFormat object.

DecimalFormat f = new DecimalFormat("$#0.00");
System.out.println(form.format(42.2734));

[url=http://java.sun.com/j2se/1.4.2/docs/api/java/text/DecimalFormat.html]The reference page.
