java.math.BigInteger.longValueExact() se introdujo en Java 8. Es una función incorporada que convierte el valor de BigInteger en largo y busca información perdida. Si el valor de BigInteger es mayor que 9,223,372,036,854,775,807 o menor que -9,223,372,036,854,775,808; el método lanzará ArithmeticException ya que BigInteger no cabe en un rango largo.
Sintaxis:
public long longValueExact()
Valor devuelto: este método devuelve el valor largo de este BigInteger.
Excepción: el método lanza ArithmeticException si el valor de BigInteger es mayor que 9,223,372,036,854,775,807 o menor que -9,223,372,036,854,775,808; ya que este rango no encaja en el rango largo.
Ejemplo:
Input: 98169894145 Output: 98169894145 Explanation: 98169894145 is given as input which is BigInteger and long value of 98169894145 is 98169894145 Input: -65416518651 Output: -65416518651 Explanation: -65416518651 is given as input which is BigInteger and long value of -65416518651 is -65416518651 Input: 10000000000000000000 Output: ArithmeticException Explanation: When 10000000000000000000 is tried to convert to long, since 10000000000000000000 > 9,223,372,036,854,775,807 (greater than a long's range), therefore it throws an arithmetic exception.
Los siguientes programas ilustran el método longValueExact() de la clase BigInteger:
Programa 1: Para demostrar el método longValueExact() para un número positivo <9,223,372,036,854,775,807
// Java program to demonstrate longValueExact() // method of BigInteger Class import java.math.*; public class GFG { public static void main(String[] args) { // Creating a BigInteger object BigInteger big; big = new BigInteger("98169894145"); // print value of BigInteger System.out.println("BigInteger value : " + big); // convert big to the long value long b1 = big.longValueExact(); // print long value System.out.println("long converted value : " + b1); } }
BigInteger value : 98169894145 long converted value : 98169894145
Programa 2: Para demostrar el método longValueExact() para números negativos >-9,223,372,036,854,775,808
// Java program to demonstrate longValueExact() // method of BigInteger Class import java.math.*; public class GFG { public static void main(String[] args) { // Creating a BigInteger object BigInteger big = new BigInteger("-65416518651"); // print value of BigInteger System.out.println("BigInteger value : " + big); // convert big to the long value long b1 = big.longValueExact(); // print long value System.out.println("long converted value : " + b1); } }
BigInteger value : -65416518651 long converted value : -65416518651
Programa 3: Para demostrar el método longValueExact() para el número negativo <-9,223,372,036,854,775,808. Lanzará una excepción aritmética
// Java program to demonstrate longValueExact() // method of BigInteger Class import java.math.*; public class GFG { public static void main(String[] args) { // Creating a BigInteger object BigInteger big = new BigInteger("-10000000000000000000"); // print value of BigInteger System.out.println("BigInteger value : " + big); try { // convert big to the long value long b1 = big.longValueExact(); // print long value System.out.println("long converted value : " + b1); } catch (Exception e) { System.out.println("Exception: " + e); } } }
BigInteger value : -10000000000000000000 Exception: java.lang.ArithmeticException: BigInteger out of long range
Programa 4: Para demostrar el método longValueExact() para un número positivo >9,223,372,036,854,775,807. Lanzará una excepción aritmética
// Java program to demonstrate longValueExact() // method of BigInteger Class import java.math.*; public class GFG { public static void main(String[] args) { // Creating a BigInteger object BigInteger big = new BigInteger("10000000000000000000"); // print value of BigInteger System.out.println("BigInteger value : " + big); try { // convert big to the long value long b1 = big.longValueExact(); // print long value System.out.println("long converted value : " + b1); } catch (Exception e) { System.out.println("Exception: " + e); } } }
BigInteger value : 10000000000000000000 Exception: java.lang.ArithmeticException: BigInteger out of long range
Referencia: https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#longValueExact–
Publicación traducida automáticamente
Artículo escrito por AmanSingh2210 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA