use of java.math.RoundingMode in project robovm by robovm.
the class BigDecimalArithmeticTest method testRoundMathContextPrecision0.
/**
* round(BigDecimal, MathContext) when precision = 0
*/
public void testRoundMathContextPrecision0() {
String a = "3736186567876876578956958765675671119238118911893939591735";
int aScale = 45;
int precision = 0;
RoundingMode rm = RoundingMode.HALF_UP;
MathContext mc = new MathContext(precision, rm);
String res = "3736186567876.876578956958765675671119238118911893939591735";
BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
BigDecimal result = aNumber.round(mc);
assertEquals("incorrect quotient value", res, result.toString());
assertEquals("incorrect quotient scale", aScale, result.scale());
}
use of java.math.RoundingMode in project jdk8u_jdk by JetBrains.
the class TieRoundingTest method formatOutputTestObject.
static void formatOutputTestObject(NumberFormat nf, Object someNumber, String tiePosition, String inputDigits, String expectedOutput) {
int mfd = nf.getMaximumFractionDigits();
RoundingMode rm = nf.getRoundingMode();
String result = nf.format(someNumber);
if (!result.equals(expectedOutput)) {
System.out.println();
System.out.println("========================================");
System.out.println("***Failure : error formatting value from string : " + inputDigits);
System.out.println("NumberFormat pattern is : " + ((DecimalFormat) nf).toPattern());
System.out.println("Maximum number of fractional digits : " + mfd);
System.out.println("Fractional rounding digit : " + (mfd + 1));
System.out.println("Position of value relative to tie : " + tiePosition);
System.out.println("Rounding Mode : " + rm);
System.out.println("Number self output representation: " + someNumber);
System.out.println("Error. Formatted result different from expected." + "\nExpected output is : \"" + expectedOutput + "\"" + "\nFormated output is : \"" + result + "\"");
System.out.println("========================================");
System.out.println();
errorCounter++;
allPassed = false;
} else {
testCounter++;
System.out.print("Success. Number input :" + inputDigits);
System.out.print(", rounding : " + rm);
System.out.print(", fract digits : " + mfd);
System.out.print(", tie position : " + tiePosition);
System.out.println(", expected : " + expectedOutput);
}
}
use of java.math.RoundingMode in project jdk8u_jdk by JetBrains.
the class TieRoundingTest method main.
public static void main(String[] args) {
// The 3 HALF_* rounding modes are impacted by bugs 7131459, 8039915.
// So we do not test the other rounding modes.
RoundingMode[] roundingModes = { RoundingMode.HALF_DOWN, RoundingMode.HALF_EVEN, RoundingMode.HALF_UP };
// Precise the relative position of input value against its closest tie.
// The double values tested below for 3 and 5 fractional digits must follow
// this scheme (position toward tie).
String[] tieRelativePositions = { "below", "exact", "above", "below", "exact", "above", "below", "exact", "above", "below", "above", "above", "below", "below", "above", "below", "exact", "above" };
// =============== Testing double (and thus float) value cases =========
System.out.println("\n===== testing 3 digits rounding position =====");
double[] values3FractDigits = { // unimpacting values close to tie, with less than 3 input fract digits
1.115d, 1.125d, 1.135d, // HALF_* impacting close to tie values covering all 6 tie cases
0.3115d, 0.3125d, 0.3135d, 0.6865d, 0.6875d, 0.6885d, // specific HALF_UP close to tie values
0.3124d, 0.3126d, 0.3128d, // specific HALF_DOWN close to tie values
0.6864d, 0.6865d, 0.6868d, // unimpacting values close to tie, with more than 3 input fract digits
1.46885d, 2.46875d, 1.46865d };
String[] inputs3FractDigits = { "1.115d", "1.125d", "1.135d", "0.3115d", "0.3125d", "0.3135d", "0.6865d", "0.6875d", "0.6885d", "0.3124d", "0.3126d", "0.3128d", "0.6864d", "0.6865d", "0.6868d", "1.46885d", "2.46875d", "1.46865d" };
String[][] expected3FractDigits = { { "1.115", "1.125", "1.135", "0.311", "0.312", "0.314", "0.686", "0.687", "0.689", "0.312", "0.313", "0.313", "0.686", "0.686", "0.687", "1.469", "2.469", "1.469" }, { "1.115", "1.125", "1.135", "0.311", "0.312", "0.314", "0.686", "0.688", "0.689", "0.312", "0.313", "0.313", "0.686", "0.686", "0.687", "1.469", "2.469", "1.469" }, { "1.115", "1.125", "1.135", "0.311", "0.313", "0.314", "0.686", "0.688", "0.689", "0.312", "0.313", "0.313", "0.686", "0.686", "0.687", "1.469", "2.469", "1.469" } };
for (int r = 0; r < roundingModes.length; r++) {
NumberFormat dfDefault = NumberFormat.getInstance(Locale.US);
RoundingMode rmode = roundingModes[r];
dfDefault.setRoundingMode(rmode);
System.out.println("\n----- Now checking " + rmode + " rounding mode -----");
for (int i = 0; i < values3FractDigits.length; i++) {
double d = values3FractDigits[i];
String tiePosition = tieRelativePositions[i];
String input = inputs3FractDigits[i];
String expected = expected3FractDigits[r][i];
formatOutputTestDouble(dfDefault, d, tiePosition, input, expected);
}
}
System.out.println("\n===== testing 5 digits rounding position =====");
double[] values5FractDigits = { // unimpacting values close to tie, with less than 5 input fract digits
1.3135d, 1.3125d, 1.3115d, // HALF_* impacting values close to tie, covering all 6 cases
1.328115d, 1.328125d, 1.328135d, 1.796865d, 1.796875d, 1.796885d, // specific HALF_UP close to tie values
1.328124d, 1.798876d, 1.796889d, // specific HALF_DOWN close to tie values
1.328114d, 1.796865d, 1.328138d, // unimpacting values close to tie, with more than 5 input fract digits
1.3281149999999d, 1.75390625d, 1.7968750000001d };
String[] inputs5FractDigits = { "1.3135d", "1.3125d", "1.3115d", "1.328115d", "1.328125d", "1.328135d", "1.796865d", "1.796875d", "1.796885d", "1.328124d", "1.798876d", "1.796889d", "1.328114d", "1.796865d", "1.328138d", "1.3281149999999d", "1.75390625d", "1.7968750000001d" };
String[][] expected5FractDigits = { { "1.3135", "1.3125", "1.3115", "1.32811", "1.32812", "1.32814", "1.79686", "1.79687", "1.79689", "1.32812", "1.79888", "1.79689", "1.32811", "1.79686", "1.32814", "1.32811", "1.75391", "1.79688" }, { "1.3135", "1.3125", "1.3115", "1.32811", "1.32812", "1.32814", "1.79686", "1.79688", "1.79689", "1.32812", "1.79888", "1.79689", "1.32811", "1.79686", "1.32814", "1.32811", "1.75391", "1.79688" }, { "1.3135", "1.3125", "1.3115", "1.32811", "1.32813", "1.32814", "1.79686", "1.79688", "1.79689", "1.32812", "1.79888", "1.79689", "1.32811", "1.79686", "1.32814", "1.32811", "1.75391", "1.79688" } };
for (int r = 0; r < roundingModes.length; r++) {
DecimalFormat df5 = (DecimalFormat) NumberFormat.getInstance(Locale.US);
RoundingMode rmode = roundingModes[r];
df5.setRoundingMode(rmode);
System.out.println("\n----- Now checking " + rmode + " rounding mode -----");
df5.applyPattern("#,###.#####");
for (int i = 0; i < values5FractDigits.length; i++) {
double d = values5FractDigits[i];
String tiePosition = tieRelativePositions[i];
String input = inputs5FractDigits[i];
String expected = expected5FractDigits[r][i];
formatOutputTestDouble(df5, d, tiePosition, input, expected);
}
}
// ==================== Testing long value cases ====================
System.out.println("\n===== testing long values =====");
long l = 123456789012345L;
DecimalFormat dfLong = (DecimalFormat) NumberFormat.getInstance(Locale.US);
String tiePosition = "exact";
String input = "123456789012345L";
String expected = "123,456,789,012,345";
String result = dfLong.format(l);
formatOutputTestLong(dfLong, l, tiePosition, input, expected);
dfLong.applyPattern("0.###E0");
expected = "1.235E14";
formatOutputTestLong(dfLong, l, tiePosition, input, expected);
l = 123450000000000L;
input = "123450000000000L";
expected = "1.234E14";
formatOutputTestLong(dfLong, l, tiePosition, input, expected);
l = 987750000000000L;
input = "987750000000000L";
expected = "9.878E14";
formatOutputTestLong(dfLong, l, tiePosition, input, expected);
dfLong.applyPattern("#,###.0E0");
l = 987755000000000L;
input = "987755000000000L";
expected = "987.76E12";
formatOutputTestLong(dfLong, l, tiePosition, input, expected);
// ================= Testing BigInteger value cases =================
System.out.println("\n===== testing BigInteger values =====");
String stringValue = "12345678901234567890123456789012345";
BigInteger bi = new BigInteger(stringValue);
DecimalFormat dfBig = (DecimalFormat) NumberFormat.getInstance(Locale.US);
tiePosition = "exact";
input = stringValue;
expected = "12,345,678,901,234,567,890,123,456,789,012,345";
formatOutputTestObject(dfBig, bi, tiePosition, input, expected);
dfBig.applyPattern("0.###E0");
expected = "1.235E34";
formatOutputTestObject(dfBig, bi, tiePosition, input, expected);
stringValue = "12345000000000000000000000000000000";
input = stringValue;
bi = new BigInteger(stringValue);
expected = "1.234E34";
formatOutputTestObject(dfBig, bi, tiePosition, input, expected);
stringValue = "12345000000000000000000000000000001";
input = stringValue;
bi = new BigInteger(stringValue);
expected = "1.235E34";
formatOutputTestObject(dfBig, bi, tiePosition, input, expected);
stringValue = "98755000000000000000000000000000000";
input = stringValue;
bi = new BigInteger(stringValue);
expected = "9.876E34";
formatOutputTestObject(dfBig, bi, tiePosition, input, expected);
dfLong.applyPattern("#,###.0E0");
stringValue = "98775500000000000000000000000000000";
input = stringValue;
expected = "987.76E34";
// =============== Testing BigDecimal value cases ================
System.out.println("\n===== testing BigDecimal values =====");
dfBig = (DecimalFormat) NumberFormat.getInstance(Locale.US);
stringValue = "0.68850000000000000088817841970012523233890533447265625";
BigDecimal bd = new BigDecimal(stringValue);
tiePosition = "exact";
input = stringValue;
expected = "0.689";
formatOutputTestObject(dfBig, bd, tiePosition, input, expected);
stringValue = "0.31149999999999999911182158029987476766109466552734375";
bd = new BigDecimal(stringValue);
dfBig.applyPattern("#,##0.####");
tiePosition = "exact";
input = stringValue;
expected = "0.3115";
formatOutputTestObject(dfBig, bd, tiePosition, input, expected);
// ==================== Printing results and exiting ===================
System.out.println();
System.out.println("==> " + testCounter + " tests passed successfully");
System.out.println("==> " + errorCounter + " tests failed");
System.out.println();
if (allPassed) {
System.out.println("Success in formating all the values with the given parameters");
} else {
String s = "Test failed with " + errorCounter + " formating error(s).";
System.out.println(s);
throw new RuntimeException(s);
}
}
use of java.math.RoundingMode in project jdk8u_jdk by JetBrains.
the class TieRoundingTest method formatOutputTestDouble.
static void formatOutputTestDouble(NumberFormat nf, double doubleToTest, String tiePosition, String inputDigits, String expectedOutput) {
int mfd = nf.getMaximumFractionDigits();
RoundingMode rm = nf.getRoundingMode();
String result = nf.format(doubleToTest);
if (!result.equals(expectedOutput)) {
System.out.println();
System.out.println("========================================");
System.out.println("***Failure : error formatting value from string : " + inputDigits);
System.out.println("NumberFormat pattern is : " + ((DecimalFormat) nf).toPattern());
System.out.println("Maximum number of fractional digits : " + mfd);
System.out.println("Fractional rounding digit : " + (mfd + 1));
System.out.println("Position of value relative to tie : " + tiePosition);
System.out.println("Rounding Mode : " + rm);
System.out.println("BigDecimal output : " + new BigDecimal(doubleToTest).toString());
System.out.println("FloatingDecimal output : " + doubleToTest);
System.out.println("Error. Formatted result different from expected." + "\nExpected output is : \"" + expectedOutput + "\"" + "\nFormated output is : \"" + result + "\"");
System.out.println("========================================");
System.out.println();
errorCounter++;
allPassed = false;
} else {
testCounter++;
System.out.println("\nSuccess for double value : " + doubleToTest + " :");
System.out.println(" Input digits :" + inputDigits + ", BigDecimal value : " + new BigDecimal(doubleToTest).toString());
System.out.print(" Rounding mode: " + rm);
System.out.print(", fract digits : " + mfd);
System.out.print(", position : " + tiePosition + " tie");
System.out.print(", result : " + result);
System.out.println(", expected : " + expectedOutput);
}
}
use of java.math.RoundingMode in project jdk8u_jdk by JetBrains.
the class RoundingAndPropertyTest method testSettersAndFastPath.
/* This methods checks the behaviour of the management of properties
* of a DecimalFormat instance that satisfies fast-path constraints.
*
* It does this by comparing the results of the format(double) output
* obtained from initial fast-path state with the output provided by
* the same instance that has been pushed and exercised outside
* fast-path rules and finally "reverted" to its initial fast-path state.
*
* The schema of actions is this :
* - Call format(double) on a known DecimalFormat fast-path instance,
* and store this result.
* - Record the current state of a given property.
* - Change the property to invalidate the fast-path state.
* - Call again format(double) on the instance.
* - Revert state of property to validate again fast-path context.
* - Call format(double) again.
* - Check that first and last call to format(double) provide same result
* - Record failure if any.
* - Do the same for another property with the same instance.
* So all the property changes are chained one after the other on only the
* same instance.
*
* Some properties that currently do not influence the fast-path state
* are also tested. This is not useful with current fast-path source
* but is here for testing the whole set of properties. This is the case
* for prefixes and suffixes, and parseBigDecimal properties.
*/
private static int testSettersAndFastPath(DecimalFormat df, boolean isCurrency) {
final double d1 = GoldenDoubleValues.PROPERTY_CHECK_POSITIVE_VALUE;
final double d2 = GoldenDoubleValues.PROPERTY_CHECK_NEGATIVE_VALUE;
int errors = 0;
boolean testSucceeded = false;
String firstFormatResult;
String secondFormatResult;
String propertyName;
// ---- positivePrefix property test ----
testSucceeded = false;
propertyName = "positivePrefix";
System.out.print("Checking " + propertyName + " property.");
String initialPrefix = df.getPositivePrefix();
firstFormatResult = df.format(d1);
df.setPositivePrefix("positivePrefix:");
df.format(d1);
df.setPositivePrefix(initialPrefix);
secondFormatResult = df.format(d1);
testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
if (!testSucceeded)
errors++;
// ---- positiveSuffix property test ----
testSucceeded = false;
propertyName = "positiveSuffix";
System.out.print("Checking " + propertyName + " property.");
String initialSuffix = df.getPositiveSuffix();
firstFormatResult = df.format(d1);
df.setPositiveSuffix("positiveSuffix:");
df.format(d1);
df.setPositiveSuffix(initialSuffix);
secondFormatResult = df.format(d1);
testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
if (!testSucceeded)
errors++;
// ---- negativePrefix property test ----
testSucceeded = false;
propertyName = "negativePrefix";
System.out.print("Checking " + propertyName + " property.");
initialPrefix = df.getNegativePrefix();
firstFormatResult = df.format(d1);
df.setNegativePrefix("negativePrefix:");
df.format(d1);
df.setNegativePrefix(initialPrefix);
secondFormatResult = df.format(d1);
testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
if (!testSucceeded)
errors++;
// ---- negativeSuffix property test ----
testSucceeded = false;
propertyName = "negativeSuffix";
System.out.print("Checking " + propertyName + " property.");
initialSuffix = df.getNegativeSuffix();
firstFormatResult = df.format(d1);
df.setNegativeSuffix("negativeSuffix:");
df.format(d1);
df.setNegativeSuffix(initialSuffix);
secondFormatResult = df.format(d1);
testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
if (!testSucceeded)
errors++;
// ---- multiplier property test ----
testSucceeded = false;
propertyName = "multiplier";
System.out.print("Checking " + propertyName + " property.");
int initialMultiplier = df.getMultiplier();
firstFormatResult = df.format(d1);
df.setMultiplier(10);
df.format(d1);
df.setMultiplier(initialMultiplier);
secondFormatResult = df.format(d1);
testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
if (!testSucceeded)
errors++;
// ---- groupingUsed property test ----
testSucceeded = false;
propertyName = "groupingUsed";
System.out.print("Checking " + propertyName + " property.");
boolean initialGroupingUsed = df.isGroupingUsed();
firstFormatResult = df.format(d1);
df.setGroupingUsed(!initialGroupingUsed);
df.format(d1);
df.setGroupingUsed(initialGroupingUsed);
secondFormatResult = df.format(d1);
testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
if (!testSucceeded)
errors++;
// ---- groupingSize property test ----
testSucceeded = false;
propertyName = "groupingSize";
System.out.print("Checking " + propertyName + " property.");
int initialGroupingSize = df.getGroupingSize();
firstFormatResult = df.format(d1);
df.setGroupingSize(initialGroupingSize + 1);
df.format(d1);
df.setGroupingSize(initialGroupingSize);
secondFormatResult = df.format(d1);
testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
if (!testSucceeded)
errors++;
// ---- decimalSeparatorAlwaysShown property test ----
testSucceeded = false;
propertyName = "decimalSeparatorAlwaysShown";
System.out.print("Checking " + propertyName + " property.");
boolean initialDSShown = df.isDecimalSeparatorAlwaysShown();
firstFormatResult = df.format(d1);
df.setDecimalSeparatorAlwaysShown(!initialDSShown);
df.format(d1);
df.setDecimalSeparatorAlwaysShown(initialDSShown);
secondFormatResult = df.format(d1);
testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
if (!testSucceeded)
errors++;
// ---- parseBigDecimal property test ----
testSucceeded = false;
propertyName = "parseBigDecimal";
System.out.print("Checking " + propertyName + " property.");
boolean initialParseBigdecimal = df.isParseBigDecimal();
firstFormatResult = df.format(d1);
df.setParseBigDecimal(!initialParseBigdecimal);
df.format(d1);
df.setParseBigDecimal(initialParseBigdecimal);
secondFormatResult = df.format(d1);
testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
if (!testSucceeded)
errors++;
// ---- maximumIntegerDigits property test ----
testSucceeded = false;
propertyName = "maximumIntegerDigits";
System.out.print("Checking " + propertyName + " property.");
int initialMaxIDs = df.getMaximumIntegerDigits();
firstFormatResult = df.format(d1);
df.setMaximumIntegerDigits(8);
df.format(d1);
df.setMaximumIntegerDigits(initialMaxIDs);
secondFormatResult = df.format(d1);
testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
if (!testSucceeded)
errors++;
// ---- minimumIntegerDigits property test ----
testSucceeded = false;
propertyName = "minimumIntegerDigits";
System.out.print("Checking " + propertyName + " property.");
int initialMinIDs = df.getMinimumIntegerDigits();
firstFormatResult = df.format(d1);
df.setMinimumIntegerDigits(2);
df.format(d1);
df.setMinimumIntegerDigits(initialMinIDs);
secondFormatResult = df.format(d1);
testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
if (!testSucceeded)
errors++;
// ---- maximumFractionDigits property test ----
testSucceeded = false;
propertyName = "maximumFractionDigits";
System.out.print("Checking " + propertyName + " property.");
firstFormatResult = df.format(d1);
df.setMaximumFractionDigits(8);
df.format(d1);
if (isCurrency) {
df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(2);
} else {
df.setMinimumFractionDigits(0);
df.setMaximumFractionDigits(3);
}
secondFormatResult = df.format(d1);
testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
if (!testSucceeded)
errors++;
// ---- minimumFractionDigits property test ----
testSucceeded = false;
propertyName = "minimumFractionDigits";
System.out.print("Checking " + propertyName + " property.");
firstFormatResult = df.format(d1);
df.setMinimumFractionDigits(1);
df.format(d1);
if (isCurrency) {
df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(2);
} else {
df.setMinimumFractionDigits(0);
df.setMaximumFractionDigits(3);
}
secondFormatResult = df.format(d1);
testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
if (!testSucceeded)
errors++;
// ---- currency property test ----
testSucceeded = false;
propertyName = "currency";
System.out.print("Checking " + propertyName + " property.");
Currency initialCurrency = df.getCurrency();
Currency japanCur = java.util.Currency.getInstance(Locale.JAPAN);
firstFormatResult = df.format(d1);
df.setCurrency(japanCur);
df.format(d1);
df.setCurrency(initialCurrency);
secondFormatResult = df.format(d1);
testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
if (!testSucceeded)
errors++;
// ---- roundingMode property test ----
testSucceeded = false;
propertyName = "roundingMode";
System.out.print("Checking " + propertyName + " property.");
RoundingMode initialRMode = df.getRoundingMode();
firstFormatResult = df.format(d1);
df.setRoundingMode(RoundingMode.HALF_UP);
df.format(d1);
df.setRoundingMode(RoundingMode.HALF_EVEN);
secondFormatResult = df.format(d1);
testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
if (!testSucceeded)
errors++;
// ---- decimalFormatSymbols property test ----
testSucceeded = false;
propertyName = "decimalFormatSymbols";
System.out.print("Checking " + propertyName + " property.");
DecimalFormatSymbols initialDecimalFormatSymbols = df.getDecimalFormatSymbols();
firstFormatResult = df.format(d1);
Locale bizarreLocale = new Locale("fr", "FR");
DecimalFormatSymbols unusualSymbols = new DecimalFormatSymbols(bizarreLocale);
unusualSymbols.setDecimalSeparator('@');
unusualSymbols.setGroupingSeparator('|');
df.setDecimalFormatSymbols(unusualSymbols);
df.format(d1);
df.setDecimalFormatSymbols(initialDecimalFormatSymbols);
secondFormatResult = df.format(d1);
testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
if (!testSucceeded)
errors++;
testSucceeded = false;
System.out.print("Checking " + propertyName + " property.");
initialDecimalFormatSymbols = df.getDecimalFormatSymbols();
firstFormatResult = df.format(d1);
Locale japanLocale = Locale.JAPAN;
unusualSymbols = new DecimalFormatSymbols(japanLocale);
unusualSymbols.setDecimalSeparator('9');
unusualSymbols.setGroupingSeparator('0');
df.setDecimalFormatSymbols(unusualSymbols);
df.format(d1);
df.setDecimalFormatSymbols(initialDecimalFormatSymbols);
secondFormatResult = df.format(d1);
testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
if (!testSucceeded)
errors++;
return errors;
}
Aggregations