Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: java help

  1. #1
    New Coder
    Join Date
    Jan 2004
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    New Coder
    Join Date
    Nov 2003
    Location
    Northwest
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #4
    Regular Coder
    Join Date
    Dec 2003
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •