...

java help

VietBoyVS
04-26-2004, 07:43 AM
Hi guys & gals,

Is there a way in Java that I can convert a double type number such as 3.1111111111222 to only two decimal places? So it would be as 3.11 only?

Another question is how to do Stop a method from running in Java Code?

For example.. I'm doing a loop.. and a user input a 'char' type.
If it's match.. it stop excute.. other while it keeps going.

I know have to use input, and if and else statement.

Which I already did.. But I need a method to stop from running..

Please help...

Thank you so much.

shmoove
04-26-2004, 09:19 AM
I'm not sure about your first question but I'm sure if you check the documentation for the Math class, and the Double and Float classes you'll find something. As a last resort you can always multiply the number by 100, floor() or round() it to an integer, and then divide by 100.

About the second question:
You should add a variable that tells the loop if it should continue running. Then you write you loop like so:

while (running) {
// do something
}

Then the input handler just has to set the variable running to false, and the execution of the loop won't continue.

shmoove

lostinjava
05-20-2004, 04:29 PM
something tells me you are tryin to find pi using the vieta method...i had to do a similar assignment at uni.

check out the double class at...

http://java.sun.com/j2se/1.4/docs/api/index.html

there should be a roundup/down method attached somewhere

warhammerdude20
05-22-2004, 11:12 PM
[code]
int the_number=3.1111111111222;
//the number...

String num=""+3.1111111111222+"";
//turn the number to a string

String new=num.indexOf(0)+num.indexOf(1)+num.indexOf(2)+num.indexOf(3);
//grabs the first 4 characters(including the dot)

int final_number=parseInt(new);
//turns the string to an integer

thats a bad way to do it, but it will work



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum