Está en la página 1de 2

Java BigInteger example Posted on: November 4, 2008 at 12:00 AM BigInteger class provides us operations for modular arithmetic

(such as add, su btract) , prime generation , bit manipulation and many other useful operations. Java BigInteger example BigInteger class provides us operations for modular arithmetic (such as add, sub tract) , prime generation , bit manipulation and many other useful operations. Here in this example of BigInteger we have created two big integer variables and we are going to multiply them and will store that result in another BigInteger variable. Here is the full example code of BigIntegerExample.java as follows: BigIntegerExample.java import java.math.BigInteger; public class BigIntegerExample { public static void main(String[] args) { BigInteger bigInteger1 = new BigInteger ("123456789"); BigInteger bigInteger2 = new BigInteger ("112334"); BigInteger bigIntResult = bigInteger1.multiply(bigInteger2); System.out.println("Result is ==> " + bigIntResult); } } Output of the above program is as follows: C:\biginteger>javac BigIntegerExample.java C:\biginteger>java BigIntegerExample Result is ==> 13868394935526

Java: Truncar o Redondear un Numero Double a n Decimales September 4th, 2008 Hace unos dias tuve la necesidad de redondear y truncar un numero Double; total que me puse creativo y termine usando un metodo basado en Strings el cual si bien s i me funcionaba tenia un sinfin de excepciones o mejor dicho particularidades que si era negativo, que si era mayor a tal numero etc. Eso me llevo a probar otra form a pero basado en DecimalFormat, la cual en la mayoria de los casos terminaba red ondeando. Para no hacerlo muy largo este post, les dejo los metodos que ensamble apartir d e pedazos de codigo que encontre en la red. Redondear un numero a n Decimales public double Redondear(double nD, int nDec) { return Math.round(nD*Math.pow(10,nDec))/Math.pow(10,nDec); } Truncar un numero a n Decimales

public double Truncar(double nD, int nDec) { if(nD > 0) nD = Math.floor(nD * Math.pow(10,nDec))/Math.pow(10,nDec); else nD = Math.ceil(nD * Math.pow(10,nDec))/Math.pow(10,nDec); return nD; } Ya saben cualquier duda comentario o sugerencia la pueden dejar como comentario; espero les sirvan estos metodos, los cuales probe y efectivamente me funcionaro n. a

También podría gustarte