Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 formatting...
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
lionheart




PostPosted: Mon Jan 24, 2005 10:13 pm   Post subject: 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
Sponsor
Sponsor
Sponsor
sponsor
Hikaru79




PostPosted: Mon Jan 24, 2005 10:17 pm   Post subject: (No subject)

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

Java:

myDouble = 64.56945;
myDouble = (double) (Math.round (myDouble*100))/100;


That should work =) Remember, comp sci is all about creative problem solving ^_^
lionheart




PostPosted: Mon Jan 24, 2005 10:20 pm   Post subject: 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




PostPosted: Mon Jan 24, 2005 10:26 pm   Post subject: yea that actually did...

that did work... thanks a million hikaru
i ve spent so long on this damn final project....
Hikaru79




PostPosted: Mon Jan 24, 2005 10:27 pm   Post subject: (No subject)

No problem, Lionheart =) Pass it on! Very Happy
lionheart




PostPosted: Mon Jan 24, 2005 10:46 pm   Post subject: 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




PostPosted: Mon Jan 24, 2005 10:53 pm   Post subject: Re: well is it..

lionheart wrote:
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




PostPosted: Mon Jan 24, 2005 10:59 pm   Post subject: 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
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Tue Jan 25, 2005 12:23 am   Post subject: (No subject)

A better solution would be to use a DecimalFormat object.

First you need to import java.text.

code:
import java.text.*;


Then you create a DecimalFormat object.

code:
DecimalFormat f = new DecimalFormat("$#0.00");
System.out.println(form.format(42.2734));


The reference page.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: