0

Hey I have a problem with number representation. Code is working properly but result is 2.516415E7 number with is good result but I prefer normal representation like 25164150. Unfortunately I can't use long type because I used Math.pow in code( which works only in double type) It is possible? How can I do it in Java?

this is my code:

//Find the difference between the sum of the squares of the first one hundred natural numbers
//and the square of the sum.

public class Sum_square_difference {
    public double Sum_of_the_squares() {
        double sum = 0;

        for (int i = 1; i <= 100; i++) {
            double squares = 0;

            squares = Math.pow(i, 2);
            sum += squares;
        }
        return sum;
    }

    public double Sum_of_the_natural_numbers() {
        double result = 0;
        double sum = 0;
        for (int i = 1; i <= 100; i++) {
            sum += i;
        }
        result = Math.pow(sum, 2);
        return result;
    }

    public static void main(String[] args) {
        Sum_square_difference sqare = new Sum_square_difference();
        System.out.println(sqare.Sum_of_the_squares());
        System.out.println(sqare.Sum_of_the_natural_numbers());
        System.out.println(sqare.Sum_of_the_natural_numbers() - sqare.Sum_of_the_squares());
    }
}
Marv
  • 3,517
  • 2
  • 22
  • 47
Jasuri
  • 11
  • 4
  • 4
    http://stackoverflow.com/questions/16098046/how-to-print-double-value-without-scientific-notation-using-java – Reimeus Mar 16 '16 at 21:09

1 Answers1

1

One possible option, use formatted io. Something like,

double val = 2.516415E7;
System.out.printf("%.0f%n", val);

output is (as requested)

25164150
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249