Converter float ou double to BigInteger. Só preciso de um exemplo

2 respostas
C

Olá!

Encontrei esse código que explica como converter qualquer tipo de numero em BigInteger ou BigDecimal. Mas não consigo implementar, se alguém puder escrever um exemplo tipo double d = 0.86 e converte-lo em BigInteger ficarei muito grato!

/*

  • NumericConverterImpl.java
  • Created on March 21, 2003
    /
    /

    package com.sun.jdo.spi.persistence.support.sqlstore.utility;

import java.math.BigDecimal;

// import java.math.BigInteger;
/
/
*

  • This is a concrete implementation class for numeric conversion to BigDecimal
  • or BigInteger. For conversion to BigInteger, we truncate the fraction
  • part of the number.
  • @author Shing Wai Chan
    */
/*

public class NumericConverterImpl implements NumericConverter {

/**

* @param policy for determine mechanism for conversion to BigDecimal

*        and BigInteger

<em>/

/</em>  public NumericConverterImpl() {

}

<em>/

/</em>*

* To convert BigInteger to BigDecimal.

* @param bInteger the BigInteger to be converted

* @return converted BigDecimal

<em>/

/</em>     public BigDecimal toBigDecimal(BigInteger bInteger) {

return (bInteger == null) ? null : new BigDecimal(bInteger);

}
/**
  * To convert Double to BigDecimal.
  * @param d the Double to be converted
  * @return converted BigDecimal
  */
/*     public BigDecimal toBigDecimal(Double d) {

return (d == null) ? null : new BigDecimal(d.toString());

}
/**
  * To convert Float to BigDecimal.
  * @param f the Float to be converted
  * @return converted BigDecimal
  */
/*    public BigDecimal toBigDecimal(Float f) {

return (f == null) ? null : new BigDecimal(f.toString());

}
/**
  * To convert Number other than BigInteger, Double and Float to BigDecimal.
  * @param n the Number to be converted
  * @return converted BigDecimal
  */
/*   public BigDecimal toBigDecimal(Number n) {

return (n == null) ? null : new BigDecimal(n.toString());

}
/**
  * To convert BigDecimal to BigInteger.
  * @param bDecimal the BigDecimal to be converted
  * @return converted BigInteger
  */
/*   public BigInteger toBigInteger(BigDecimal bDecimal) {

return (bDecimal == null) ? null : bDecimal.toBigInteger();

}
/**
  * To convert Double to BigInteger.
  * @param d the Double to be converted
  * @return converted BigInteger
  */
/*     public BigInteger toBigInteger(Double d) {

return (d == null) ? null : (new BigDecimal(d.toString())).toBigInteger();

}
/**
  * To convert Float to BigInteger.
  * @param f the Float to be converted
  * @return converted BigInteger
  */
/*    public BigInteger toBigInteger(Float f) {

return (f == null) ? null : (new BigDecimal(f.toString())).toBigInteger();

}
/**
  * To convert Number other than BigDecimal, Double and Float to BigInteger.
  * @param n the Number to be converted
  * @return converted BigInteger
  */
/*   public BigInteger toBigInteger(Number n) {

return (n == null) ? null : BigInteger.valueOf(n.longValue());

}

}

*/

2 Respostas

B

Não entendi o seu problema, pois você mesmo já postou a solução. Segue o exemplo de como utilizar o próprio método do seu exemplo.

PS. Vale lembrar que ao fazer a conversão do double para BigInteger as casas decimais serão truncadas.

import java.math.BigDecimal;
import java.math.BigInteger;

public class Diversos {

	public static void main(String[] args) {
		Diversos diversos = new  Diversos();
		double d = 10.86; 
		System.out.println(diversos.toBigInteger(new Double(d)));
	}

	public BigInteger toBigInteger(Double d) {
		return (d == null) ? null : (new BigDecimal(d.toString()))
				.toBigInteger();
	}
}
B

Completando, e talvez seja esta a sua dúvida.

A classe BigInteger não aceita como parâmetro um double ou Double(). Perceba que é utilizado a Classe BigDecimal e retornado somente a parte inteira.

Criado 5 de maio de 2008
Ultima resposta 6 de mai. de 2008
Respostas 2
Participantes 2