List of usage examples for java.math BigDecimal toBigIntegerExact
public BigInteger toBigIntegerExact()
From source file:Main.java
public static void main(String[] args) { BigDecimal bg1 = new BigDecimal("2426"); // assign the BigIntegerExact value of bg1 to i1 BigInteger i1 = bg1.toBigIntegerExact(); System.out.println("BigInteger value of " + bg1 + " is " + i1); }
From source file:com.github.fge.jsonschema.keyword.digest.helpers.NumericDigester.java
protected final ObjectNode digestedNumberNode(final JsonNode schema) { final ObjectNode ret = FACTORY.objectNode(); final JsonNode node = schema.get(keyword); final boolean isLong = valueIsLong(node); ret.put("valueIsLong", isLong); if (isLong) { ret.put(keyword, node.canConvertToInt() ? FACTORY.numberNode(node.intValue()) : FACTORY.numberNode(node.longValue())); return ret; }// w w w .j a v a 2 s .c o m final BigDecimal decimal = node.decimalValue(); ret.put(keyword, decimal.scale() == 0 ? FACTORY.numberNode(decimal.toBigIntegerExact()) : node); return ret; }
From source file:pl.psnc.synat.wrdz.realm.db.WrdzUserDatabaseHandler.java
/** * Authenticates user using username and password he provided and comparing it to the data in the user database. * /*w w w. j av a 2 s. co m*/ * @param username * name of the user who is to be authenticated. * @param password * password of the user who is to be authenticated. * @return whether or not user data is valid (passed user data matches data in the database). */ public boolean isUserValid(String username, char[] password) { Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; boolean valid = false; byte[] salt = null; try { connection = getConnection(); statement = connection.prepareStatement(saltQuery); statement.setString(1, username); resultSet = statement.executeQuery(); if (resultSet.next()) { BigDecimal decimalSalt = resultSet.getBigDecimal(1); if (decimalSalt != null) { salt = decimalSalt.toBigIntegerExact().toByteArray(); } } } catch (SQLException ex) { logger.log(Level.SEVERE, "Cannot validate user " + username + ", exception: " + ex.toString()); if (logger.isLoggable(Level.FINE)) { logger.log(Level.FINE, "Cannot validate user", ex); } } catch (Exception ex) { logger.log(Level.SEVERE, "Invalid user " + username); if (logger.isLoggable(Level.FINE)) { logger.log(Level.FINE, "Cannot validate user", ex); } } finally { close(connection, statement, resultSet); } try { char[] hashedPassword = digestAuthHandler.hashPassword(password, salt); connection = getConnection(); statement = connection.prepareStatement(passwordQuery); statement.setString(1, username); resultSet = statement.executeQuery(); if (resultSet.next()) { Reader reader = resultSet.getCharacterStream(1); char[] retrievedPassword = extractFromReader(reader); valid = digestAuthHandler.comparePasswords(hashedPassword, retrievedPassword); } } catch (SQLException ex) { logger.log(Level.SEVERE, "Cannot validate user " + username + ", exception: " + ex.toString()); if (logger.isLoggable(Level.FINE)) { logger.log(Level.FINE, "Cannot validate user", ex); } } catch (Exception ex) { logger.log(Level.SEVERE, "Invalid user " + username); if (logger.isLoggable(Level.FINE)) { logger.log(Level.FINE, "Cannot validate user", ex); } } finally { close(connection, statement, resultSet); } return valid; }
From source file:org.kjots.json.content.io.simple.SimpleJsonReader.java
/** * Create the content handler./*ww w. ja v a 2 s. c om*/ * * @return The content handler. */ private ContentHandler createContentHandler() { return new ContentHandler() { @Override public void startJSON() { SimpleJsonReader.this.jsonContentHandler.startJson(); } @Override public void endJSON() { SimpleJsonReader.this.jsonContentHandler.endJson(); } @Override public boolean startObject() { SimpleJsonReader.this.jsonContentHandler.startObject(); return true; } @Override public boolean endObject() { SimpleJsonReader.this.jsonContentHandler.endObject(); return true; } @Override public boolean startObjectEntry(String key) { SimpleJsonReader.this.jsonContentHandler.memberName(key); return true; } @Override public boolean endObjectEntry() { return true; } @Override public boolean startArray() { SimpleJsonReader.this.jsonContentHandler.startArray(); return true; } @Override public boolean endArray() { SimpleJsonReader.this.jsonContentHandler.endArray(); return true; } @Override public boolean primitive(Object value) { if (value instanceof BigDecimal) { BigDecimal numericValue = (BigDecimal) value; // Attempt to coerce the value into an integer try { value = Integer.valueOf(numericValue.intValueExact()); } catch (ArithmeticException ae1) { // Attempt to coerce the value into a long try { value = Long.valueOf(numericValue.longValueExact()); } catch (ArithmeticException ae2) { // Attempt to coerce the value into a BigInteger try { value = numericValue.toBigIntegerExact(); } catch (ArithmeticException ae3) { // Ignore this exception - value will remain a BigDecimal } } } } SimpleJsonReader.this.jsonContentHandler.primitive(value); return true; } }; }
From source file:net.pms.util.Rational.java
/** * Returns an instance that represents the value of {@code value}. * * @param value the value.//from w ww. ja va 2 s . c om * @return An instance that represents the value of {@code value}. */ @Nullable public static Rational valueOf(@Nullable BigDecimal value) { if (value == null) { return null; } BigInteger numerator; BigInteger denominator; if (value.signum() == 0) { return ZERO; } if (BigDecimal.ONE.equals(value)) { return ONE; } if (value.scale() > 0) { BigInteger unscaled = value.unscaledValue(); BigInteger tmpDenominator = BigInteger.TEN.pow(value.scale()); BigInteger tmpGreatestCommonDivisor = unscaled.gcd(tmpDenominator); numerator = unscaled.divide(tmpGreatestCommonDivisor); denominator = tmpDenominator.divide(tmpGreatestCommonDivisor); } else { numerator = value.toBigIntegerExact(); denominator = BigInteger.ONE; } return new Rational(numerator, denominator, BigInteger.ONE, numerator, denominator); }
From source file:net.pms.util.Rational.java
/** * Returns an instance with the given {@code numerator} and * {@code denominator}.//from ww w .j a v a 2 s. c o m * * @param numerator the numerator. * @param denominator the denominator. * @return An instance that represents the value of {@code numerator}/ * {@code denominator}. */ @Nullable public static Rational valueOf(@Nullable BigDecimal numerator, @Nullable BigDecimal denominator) { if (numerator == null || denominator == null) { return null; } if (numerator.signum() == 0 && denominator.signum() == 0) { return NaN; } if (denominator.signum() == 0) { return numerator.signum() > 0 ? POSITIVE_INFINITY : NEGATIVE_INFINITY; } if (numerator.signum() == 0) { return ZERO; } if (numerator.equals(denominator)) { return ONE; } if (denominator.signum() < 0) { numerator = numerator.negate(); denominator = denominator.negate(); } int scale = Math.max(numerator.scale(), denominator.scale()); if (scale > 0) { numerator = numerator.scaleByPowerOfTen(scale); denominator = denominator.scaleByPowerOfTen(scale); } BigInteger biNumerator = numerator.toBigIntegerExact(); BigInteger biDenominator = denominator.toBigIntegerExact(); BigInteger reducedNumerator; BigInteger reducedDenominator; BigInteger greatestCommonDivisor = calculateGreatestCommonDivisor(biNumerator, biDenominator); if (BigInteger.ONE.equals(greatestCommonDivisor)) { reducedNumerator = biNumerator; reducedDenominator = biDenominator; } else { reducedNumerator = biNumerator.divide(greatestCommonDivisor); reducedDenominator = biDenominator.divide(greatestCommonDivisor); } return new Rational(biNumerator, biDenominator, greatestCommonDivisor, reducedNumerator, reducedDenominator); }