use of java.math.BigDecimal in project gravel by gravel-st.
the class DoubleExtensions method roundToRoundingMode.
private static Number roundToRoundingMode(double receiver, RoundingMode roundingMode) {
BigDecimal bigDecimal = new BigDecimal(receiver);
BigDecimal rounded = bigDecimal.setScale(0, roundingMode);
return IntegerExtensions.objectFromBigInteger(rounded.toBigInteger());
}
use of java.math.BigDecimal in project j2objc by google.
the class Scanner method nextBigDecimal.
/**
* Returns the next token as a {@code BigDecimal}. This method will block if input is
* being read. If the next token can be translated into a {@code BigDecimal}
* the following is done: All {@code Locale}-specific prefixes, group separators,
* and {@code Locale}-specific suffixes are removed. Then non-ASCII digits are
* mapped into ASCII digits via {@link Character#digit(char, int)}, and a
* negative sign (-) is added if the {@code Locale}-specific negative prefix or
* suffix was present. Finally the resulting string is passed to
* {@code BigDecimal(String) }.
*
* @return the next token as a {@code BigDecimal}.
* @throws IllegalStateException
* if this {@code Scanner} has been closed.
* @throws NoSuchElementException
* if input has been exhausted.
* @throws InputMismatchException
* if the next token can not be translated into a valid
* {@code BigDecimal}.
*/
public BigDecimal nextBigDecimal() {
checkOpen();
Object obj = cachedNextValue;
cachedNextValue = null;
if (obj instanceof BigDecimal) {
findStartIndex = cachedNextIndex;
return (BigDecimal) obj;
}
Pattern floatPattern = getFloatPattern();
String floatString = next(floatPattern);
floatString = removeLocaleInfoFromFloat(floatString);
BigDecimal bigDecimalValue;
try {
bigDecimalValue = new BigDecimal(floatString);
} catch (NumberFormatException e) {
matchSuccessful = false;
recoverPreviousStatus();
throw new InputMismatchException();
}
return bigDecimalValue;
}
use of java.math.BigDecimal in project j2objc by google.
the class DecimalFormatTest method test_formatObject.
public void test_formatObject() {
DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.US);
// format maxLong
FieldPosition pos = new FieldPosition(0);
StringBuffer out = format.format(new Long(Long.MAX_VALUE), new StringBuffer(), pos);
assertTrue("Wrong result L1: " + out, out.toString().equals("9,223,372,036,854,775,807"));
// format minLong
pos = new FieldPosition(0);
out = format.format(new Long(Long.MIN_VALUE), new StringBuffer(), pos);
assertTrue("Wrong result L2: " + out, out.toString().equals("-9,223,372,036,854,775,808"));
// format maxLong of type BigInteger
pos = new FieldPosition(0);
out = format.format(new java.math.BigInteger(String.valueOf(Long.MAX_VALUE)), new StringBuffer(), pos);
assertTrue("Wrong result BI1: " + out, out.toString().equals("9,223,372,036,854,775,807"));
// format minLong of type BigInteger
pos = new FieldPosition(0);
out = format.format(new java.math.BigInteger(String.valueOf(Long.MIN_VALUE)), new StringBuffer(), pos);
assertTrue("Wrong result BI2: " + out, out.toString().equals("-9,223,372,036,854,775,808"));
// format maxLong + 1
java.math.BigInteger big;
pos = new FieldPosition(0);
big = new java.math.BigInteger(String.valueOf(Long.MAX_VALUE)).add(new java.math.BigInteger("1"));
out = format.format(big, new StringBuffer(), pos);
assertTrue("Wrong result BI3: " + out, out.toString().equals("9,223,372,036,854,775,808"));
// format minLong - 1
pos = new FieldPosition(0);
big = new java.math.BigInteger(String.valueOf(Long.MIN_VALUE)).add(new java.math.BigInteger("-1"));
out = format.format(big, new StringBuffer(), pos);
assertTrue("Wrong result BI4: " + out, out.toString().equals("-9,223,372,036,854,775,809"));
// format big decimal
pos = new FieldPosition(0);
out = format.format(new java.math.BigDecimal("51.348"), new StringBuffer(), pos);
assertTrue("Wrong result BD1: " + out, out.toString().equals("51.348"));
// format big decimal
pos = new FieldPosition(0);
out = format.format(new java.math.BigDecimal("51"), new StringBuffer(), pos);
assertTrue("Wrong result BD2: " + out, out.toString().equals("51"));
// format big decimal Double.MAX_VALUE * 2
java.math.BigDecimal bigDecimal;
pos = new FieldPosition(0);
final String doubleMax2 = "359,538,626,972,463,141,629,054,847,463,408," + "713,596,141,135,051,689,993,197,834,953,606,314,521,560,057,077," + "521,179,117,265,533,756,343,080,917,907,028,764,928,468,642,653," + "778,928,365,536,935,093,407,075,033,972,099,821,153,102,564,152," + "490,980,180,778,657,888,151,737,016,910,267,884,609,166,473,806," + "445,896,331,617,118,664,246,696,549,595,652,408,289,446,337,476," + "354,361,838,599,762,500,808,052,368,249,716,736";
bigDecimal = new BigDecimal(Double.MAX_VALUE).add(new BigDecimal(Double.MAX_VALUE));
out = format.format(bigDecimal, new StringBuffer(), pos);
assertTrue("Wrong result BDmax2: " + out, out.toString().equals(doubleMax2));
// format big decimal Double.MIN_VALUE + Double.MIN_VALUE
// and Double.MIN_VALUE - Double.MIN_VALUE
pos = new FieldPosition(0);
bigDecimal = new BigDecimal(Double.MIN_VALUE).add(new BigDecimal(Double.MIN_VALUE));
out = format.format(bigDecimal, new StringBuffer(), pos);
bigDecimal = new BigDecimal(Float.MAX_VALUE).add(new BigDecimal(Float.MAX_VALUE));
out = format.format(bigDecimal, new StringBuffer(), pos);
final String BDFloatMax2 = "680,564,693,277,057,719,623,408,366,969,033,850,880";
assertTrue("Wrong result BDFloatMax2: " + out, out.toString().equals(BDFloatMax2));
// format big decimal Float.MIN_VALUE + Float.MIN_VALUE
// and Float.MIN_VALUE - Float.MIN_VALUE
bigDecimal = new BigDecimal(Float.MIN_VALUE).add(new BigDecimal(Float.MIN_VALUE));
out = format.format(bigDecimal, new StringBuffer(), pos);
final String BDFloatMin2 = "0";
bigDecimal = new BigDecimal(Float.MIN_VALUE).subtract(new BigDecimal(Float.MIN_VALUE));
out = format.format(bigDecimal, new StringBuffer(), pos);
assertTrue("Wrong result BDFloatMax2: " + out, out.toString().equals(BDFloatMin2));
}
use of java.math.BigDecimal in project j2objc by google.
the class DecimalFormatTest method test_parse_bigDecimal.
public void test_parse_bigDecimal() throws Exception {
// parseBigDecimal default to false
DecimalFormat form = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
assertFalse(form.isParseBigDecimal());
form.setParseBigDecimal(true);
assertTrue(form.isParseBigDecimal());
Number result = form.parse("123.123");
assertEquals(new BigDecimal("123.123"), result);
form.setParseBigDecimal(false);
assertFalse(form.isParseBigDecimal());
result = form.parse("123.123");
assertFalse(result instanceof BigDecimal);
}
use of java.math.BigDecimal in project j2objc by google.
the class DecimalFormatTest method assertDecimalFormatIsLossless.
private static void assertDecimalFormatIsLossless(double d) throws Exception {
final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US);
DecimalFormat format = new DecimalFormat("#0.#", dfs);
format.setGroupingUsed(false);
format.setMaximumIntegerDigits(400);
format.setMaximumFractionDigits(400);
// Every floating point binary can be represented exactly in decimal if you have enough
// digits. This shows the value actually being tested.
String testId = "decimalValue: " + new BigDecimal(d);
// As a sanity check we try out parseDouble() with the string generated by
// Double.toString(). Strictly speaking Double.toString() is probably not guaranteed to be
// lossless, but in reality it probably is, or at least is close enough.
assertDoubleEqual(testId + " failed parseDouble(toString()) sanity check", d, Double.parseDouble(Double.toString(d)));
// Format the number: If this is lossy it is a problem. We are trying to check that it
// doesn't lose any unnecessary precision.
String result = format.format(d);
// Here we use Double.parseDouble() which should able to parse a number we know was
// representable as a double into the original double. If parseDouble() is not implemented
// correctly the test is invalid.
double doubleParsed = Double.parseDouble(result);
assertDoubleEqual(testId + " (format() produced " + result + ")", d, doubleParsed);
// For completeness we try to parse using the formatter too. If this fails but the format
// above didn't it may be a problem with parse(), or with format() that we didn't spot.
assertDoubleEqual(testId + " failed parse(format()) check", d, format.parse(result).doubleValue());
}
Aggregations