Q:

Get approximate square root

I'm implementing Babylonian method to approximate the square root of number n using following formula :
nextGuess = (lastGuess + n / lastGuess) / 2;

So when nextGuess and lasGuess are almost identical, nextGuess is the approximated square root.
What am doing is checking if nextGuess and lastGuess is less than very small number such as 0.0001 then i can claim that nextGuess is the approximated square root of n. if not nextGuess become lastGuess.
So how i can implement that in the right way?
My current code:
public static void getApproximatedSquare(long n){
    DecimalFormat decimalFormat = new DecimalFormat("#.####");
    decimalFormat.setRoundingMode(RoundingMode.CEILING);

    double lastGuess = 1, nextGuess;

    nextGuess = (lastGuess + n / lastGuess) / 2;
    Double init =  0.0001;
    System.out.println(decimalFormat.format(init));
    if (Double.valueOf(decimalFormat.format(nextGuess)) <= init)
        //todo

}

A:

Current draft of implementation has a few flaws:

I doubt you really need Double.valueOf(decimalFormat.format(...)), it just removes some precision from the result
Your convergence condition is not nextGuess < init but difference_between_nextGuess_and_lastGuess < init
You have to repeat the approximation until convergence, so you can't use just a if. You need a for or while or (as in my solution) do... while
This should work (at each step, it prints last and next guesses)
public static double getApproximatedSquare(long n) {
    DecimalFormat decimalFormat = new DecimalFormat("#.####");
    decimalFormat.setRoundingMode(RoundingMode.CEILING);

    double lastGuess, nextGuess = 1;
    double init = 0.0001;
    do {
        lastGuess = nextGuess;
        nextGuess = (lastGuess + (double) n / lastGuess) / 2;
        System.out.println(decimalFormat.format(lastGuess)+" ---> "+decimalFormat.format(nextGuess));
    } while (Math.abs(lastGuess - nextGuess) >= init);
    return nextGuess;
}

