Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-26-2004, 07:43 AM   PM User | #1
VietBoyVS
New Coder

 
Join Date: Jan 2004
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
VietBoyVS is an unknown quantity at this point
java help

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.
VietBoyVS is offline   Reply With Quote
Old 04-26-2004, 09:19 AM   PM User | #2
shmoove
Regular Coder

 
Join Date: Dec 2003
Posts: 367
Thanks: 0
Thanked 0 Times in 0 Posts
shmoove is an unknown quantity at this point
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:
Code:
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
shmoove is offline   Reply With Quote
Old 05-20-2004, 04:29 PM   PM User | #3
lostinjava
New Coder

 
Join Date: Nov 2003
Location: Northwest
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
lostinjava is an unknown quantity at this point
VietBoy... would that be short for vieta?

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
lostinjava is offline   Reply With Quote
Old 05-22-2004, 11:12 PM   PM User | #4
warhammerdude20
Regular Coder

 
Join Date: Dec 2003
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
warhammerdude20 is an unknown quantity at this point
[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
warhammerdude20 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:39 AM.


Advertisement
Log in to turn off these ads.