Estou com o problema no calculo java

	public static void main(String[] args) throws Exception {
			double teste = 603.3d * 5.45d;
			System.out.println(teste);

			teste = ( (603.3*10d) * (5.45*10d) ) /100d;
			System.out.println(teste);
			
			teste = ( (603.3*100d) * (5.45*100d) ) /10000d;
			System.out.println(teste);
			
			teste = ( (603.3*1000d) * (5.45*1000d) ) /1000000d;
			System.out.println(teste);
			
			teste = ( (603.3*10000d) * (5.45*10000d) ) /100000000d;
			System.out.println(teste);
			
			teste = ( (603.3*100000d) * (5.45*100000d) ) /10000000000d;
			System.out.println(teste);
			
			teste = ( (603.3*1000000d) * (5.45*1000000d) ) /1000000000000d;
			System.out.println(teste);
			
			teste = ( (603.3*10000000d) * (5.45*10000000d) ) /100000000000000d;
			System.out.println(teste);
			
			teste = ( (603.3*100000000d) * (5.45*100000000d) ) /10000000000000000d;
			System.out.println(teste);
			
			teste = ( (603.3*1000000000d) * (5.45*1000000000d) ) /1000000000000000000d;
			System.out.println(teste);
	}

Saida Vai ser:

3287.9849999999997
3287.985
3287.9849999999997
3287.985
3287.985
3287.9849999999997
3287.985
3287.985
3287.9849999999997
3287.985

Entao eu pergunto… para cada calculo acima, qual é o correto? creio que os que dão 3287.985, são os corretos

Mas e os que dão 3287.9849999999997? esta incorreto? :shock:

se vc quer saber o pq basta ler o primeiro capítulo de qq livro ou apostila de cálculo numérico.

pra resolver o problema o java tem a classe BigDecimal

http://docs.oracle.com/javase/1.5.0/docs/api/java/math/BigDecimal.html

orlandocn

Isso aki tambem da pau, conforme muda a quantidade de casas decimais:

		DecimalFormat format = (DecimalFormat) NumberFormat.getInstance();
		format.applyPattern("0.0000000000000");
		System.out.println(format.format(603.3d * 5.45d));
		
		System.out.println(new BigDecimal(new Double(603.3d * 5.45d).toString()).setScale(13,BigDecimal.ROUND_HALF_UP).doubleValue() );


		DecimalFormat format = (DecimalFormat) NumberFormat.getInstance();
		format.applyPattern("0.000000000000");
		System.out.println(format.format(603.3d * 5.45d));
		
		System.out.println(new BigDecimal(new Double(603.3d * 5.45d).toString()).setScale(12,BigDecimal.ROUND_HALF_UP).doubleValue() );

Alguem tem a solução para isso?

Você precisaria neste caso saber quantas casas será usado após a virgula … fiz um exemplo usando o seu código …porém formatei os valores com 3 casas após a virgula …utilizei o number formar … o ### representa qtas casas será formatado depois da virgula … faça um teste …

[quote]public static void main(String[] args) {

	DecimalFormat df = new DecimalFormat("0.###");
	double teste = 603.3d * 5.45d;
	System.out.println("formatado1 " + df.format(teste));

	teste = ((603.3 * 10d) * (5.45 * 10d)) / 100d;
	System.out.println(teste);

	teste = ((603.3 * 100d) * (5.45 * 100d)) / 10000d;
	System.out.println("formatado2 " + df.format(teste));

	teste = ((603.3 * 1000d) * (5.45 * 1000d)) / 1000000d;
	System.out.println(teste);

	teste = ((603.3 * 10000d) * (5.45 * 10000d)) / 100000000d;
	System.out.println(teste);

	teste = ((603.3 * 100000d) * (5.45 * 100000d)) / 10000000000d;
	System.out.println(teste);

	teste = ((603.3 * 1000000d) * (5.45 * 1000000d)) / 1000000000000d;
	System.out.println(teste);

	teste = ((603.3 * 10000000d) * (5.45 * 10000000d)) / 100000000000000d;
	System.out.println(teste);

	teste = ((603.3 * 100000000d) * (5.45 * 100000000d)) / 10000000000000000d;
	System.out.println("formatado3 " + df.format(teste));

	teste = ((603.3 * 1000000000d) * (5.45 * 1000000000d)) / 1000000000000000000d;
	System.out.println("formatado4 " + df.format(teste));
}[/quote]

nao utilize

public BigDecimal(double val)

conforme a documentacao oficial:

"Notes:

  • The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the value that is being passed in to the constructor is not exactly equal to 0.1, appearances notwithstanding.

  • The String constructor, on the other hand, is perfectly predictable: writing new BigDecimal(“0.1”) creates a BigDecimal which is exactly equal to 0.1, as one would expect. Therefore, it is generally recommended that the String constructor be used in preference to this one.

  • When a double must be used as a source for a BigDecimal, note that this constructor provides an exact conversion; it does not give the same result as converting the double to a String using the Double.toString(double) method and then using the BigDecimal(String) constructor. To get that result, use the static valueOf(double) method."

utilize

public BigDecimal(String val)