use of android.icu.text.DecimalFormat in project j2objc by google.
the class IntlTestDecimalFormatAPI method testJB1871.
/**
* Problem 1: simply running
* decF4.setRoundingMode(java.math.BigDecimal.ROUND_HALF_UP) does not work
* as decF4.setRoundingIncrement(.0001) must also be run.
* Problem 2: decF4.format(8.88885) does not return 8.8889 as expected.
* You must run decF4.format(new BigDecimal(Double.valueOf(8.88885))) in
* order for this to work as expected.
* Problem 3: There seems to be no way to set half up to be the default
* rounding mode.
* We solved the problem with the code at the bottom of this page however
* this is not quite general purpose enough to include in icu4j. A static
* setDefaultRoundingMode function would solve the problem nicely. Also
* decimal places past 20 are not handled properly. A small ammount of work
* would make bring this up to snuff.
*/
@Test
public void testJB1871() {
// problem 2
double number = 8.88885;
String expected = "8.8889";
String pat = ",##0.0000";
DecimalFormat dec = new DecimalFormat(pat);
dec.setRoundingMode(BigDecimal.ROUND_HALF_UP);
double roundinginc = 0.0001;
dec.setRoundingIncrement(roundinginc);
String str = dec.format(number);
if (!str.equals(expected)) {
errln("Fail: " + number + " x \"" + pat + "\" = \"" + str + "\", expected \"" + expected + "\"");
}
pat = ",##0.0001";
dec = new DecimalFormat(pat);
dec.setRoundingMode(BigDecimal.ROUND_HALF_UP);
str = dec.format(number);
if (!str.equals(expected)) {
errln("Fail: " + number + " x \"" + pat + "\" = \"" + str + "\", expected \"" + expected + "\"");
}
// testing 20 decimal places
pat = ",##0.00000000000000000001";
dec = new DecimalFormat(pat);
BigDecimal bignumber = new BigDecimal("8.888888888888888888885");
expected = "8.88888888888888888889";
dec.setRoundingMode(BigDecimal.ROUND_HALF_UP);
str = dec.format(bignumber);
if (!str.equals(expected)) {
errln("Fail: " + bignumber + " x \"" + pat + "\" = \"" + str + "\", expected \"" + expected + "\"");
}
}
use of android.icu.text.DecimalFormat in project j2objc by google.
the class IntlTestNumberFormat method _testFormat.
/**
* call tryIt with many variations, called by testLocale
*/
private void _testFormat() {
if (fNumberFormat == null) {
errln("**** FAIL: Null format returned by createXxxInstance.");
return;
}
DecimalFormat s = (DecimalFormat) fNumberFormat;
logln("pattern :" + s.toPattern());
tryIt(-2.02147304840132e-68);
tryIt(3.88057859588817e-68);
tryIt(-2.64651110485945e+65);
tryIt(9.29526819488338e+64);
tryIt(-2.02147304840132e-100);
tryIt(3.88057859588817e-096);
tryIt(-2.64651110485945e+306);
tryIt(9.29526819488338e+250);
tryIt(-9.18228054496402e+64);
tryIt(-9.69413034454191e+64);
tryIt(-9.18228054496402e+255);
tryIt(-9.69413034454191e+273);
tryIt(1.234e-200);
tryIt(-2.3e-168);
tryIt(Double.NaN);
tryIt(Double.POSITIVE_INFINITY);
tryIt(Double.NEGATIVE_INFINITY);
tryIt(251887531);
tryIt(5e-20 / 9);
tryIt(5e20 / 9);
tryIt(1.234e-50);
tryIt(9.99999999999996);
tryIt(9.999999999999996);
tryIt(5.06e-27);
tryIt(Integer.MIN_VALUE);
tryIt(Integer.MAX_VALUE);
tryIt((double) Integer.MIN_VALUE);
tryIt((double) Integer.MAX_VALUE);
tryIt((double) Integer.MIN_VALUE - 1.0);
tryIt((double) Integer.MAX_VALUE + 1.0);
tryIt(5.0 / 9.0 * 1e-20);
tryIt(4.0 / 9.0 * 1e-20);
tryIt(5.0 / 9.0 * 1e+20);
tryIt(4.0 / 9.0 * 1e+20);
tryIt(2147483647.);
tryIt(0);
tryIt(0.0);
tryIt(1);
tryIt(10);
tryIt(100);
tryIt(-1);
tryIt(-10);
tryIt(-100);
tryIt(-1913860352);
// use test framework's random seed
Random random = createRandom();
for (int j = 0; j < 10; j++) {
double d = random.nextDouble() * 2e10 - 1e10;
tryIt(d);
}
}
use of android.icu.text.DecimalFormat in project j2objc by google.
the class IntlTestDecimalFormatAPIC method testFormatToCharacterIterator.
@Test
public void testFormatToCharacterIterator() {
Number number = new Double(350.76);
Number negativeNumber = new Double(-350.76);
Locale us = Locale.US;
// test number instance
t_Format(1, number, NumberFormat.getNumberInstance(us), getNumberVectorUS());
// test percent instance
t_Format(3, number, NumberFormat.getPercentInstance(us), getPercentVectorUS());
// test permille pattern
DecimalFormat format = new DecimalFormat("###0.##\u2030");
t_Format(4, number, format, getPermilleVector());
// test exponential pattern with positive exponent
format = new DecimalFormat("00.0#E0");
t_Format(5, number, format, getPositiveExponentVector());
// test exponential pattern with negative exponent
format = new DecimalFormat("0000.0#E0");
t_Format(6, number, format, getNegativeExponentVector());
// test currency instance with US Locale
t_Format(7, number, NumberFormat.getCurrencyInstance(us), getPositiveCurrencyVectorUS());
// test negative currency instance with US Locale
t_Format(8, negativeNumber, NumberFormat.getCurrencyInstance(us), getNegativeCurrencyVectorUS());
// test multiple grouping separators
number = new Long(100300400);
t_Format(11, number, NumberFormat.getNumberInstance(us), getNumberVector2US());
// test 0
number = new Long(0);
t_Format(12, number, NumberFormat.getNumberInstance(us), getZeroVector());
}
use of android.icu.text.DecimalFormat in project j2objc by google.
the class IntlTestDecimalFormatAPIC method TestAPI.
// This test checks various generic API methods in DecimalFormat to achieve 100% API coverage.
@Test
public void TestAPI() {
logln("DecimalFormat API test---");
logln("");
Locale.setDefault(Locale.ENGLISH);
// ======= Test constructors
logln("Testing DecimalFormat constructors");
DecimalFormat def = new DecimalFormat();
final String pattern = new String("#,##0.# FF");
final DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.FRENCH);
final CurrencyPluralInfo infoInput = new CurrencyPluralInfo(ULocale.FRENCH);
DecimalFormat pat = null;
try {
pat = new DecimalFormat(pattern);
} catch (IllegalArgumentException e) {
errln("ERROR: Could not create DecimalFormat (pattern)");
}
DecimalFormat cust1 = null;
try {
cust1 = new DecimalFormat(pattern, symbols);
} catch (IllegalArgumentException e) {
errln("ERROR: Could not create DecimalFormat (pattern, symbols)");
}
@SuppressWarnings("unused") DecimalFormat cust2 = null;
try {
cust2 = new DecimalFormat(pattern, symbols, infoInput, NumberFormat.PLURALCURRENCYSTYLE);
} catch (IllegalArgumentException e) {
errln("ERROR: Could not create DecimalFormat (pattern, symbols, infoInput, style)");
}
// ======= Test clone(), assignment, and equality
logln("Testing clone() and equality operators");
Format clone = (Format) def.clone();
if (!def.equals(clone)) {
errln("ERROR: Clone() failed");
}
// ======= Test various format() methods
logln("Testing various format() methods");
// final double d = -10456.0037; // this appears as -10456.003700000001 on NT
// final double d = -1.04560037e-4; // this appears as -1.0456003700000002E-4 on NT
// this works!
final double d = -10456.00370000000000;
final long l = 100000000;
logln("" + Double.toString(d) + " is the double value");
StringBuffer res1 = new StringBuffer();
StringBuffer res2 = new StringBuffer();
StringBuffer res3 = new StringBuffer();
StringBuffer res4 = new StringBuffer();
FieldPosition pos1 = new FieldPosition(0);
FieldPosition pos2 = new FieldPosition(0);
FieldPosition pos3 = new FieldPosition(0);
FieldPosition pos4 = new FieldPosition(0);
res1 = def.format(d, res1, pos1);
logln("" + Double.toString(d) + " formatted to " + res1);
res2 = pat.format(l, res2, pos2);
logln("" + l + " formatted to " + res2);
res3 = cust1.format(d, res3, pos3);
logln("" + Double.toString(d) + " formatted to " + res3);
res4 = cust1.format(l, res4, pos4);
logln("" + l + " formatted to " + res4);
// ======= Test parse()
logln("Testing parse()");
String text = new String("-10,456.0037");
ParsePosition pos = new ParsePosition(0);
String patt = new String("#,##0.#");
pat.applyPattern(patt);
double d2 = pat.parse(text, pos).doubleValue();
if (d2 != d) {
errln("ERROR: Roundtrip failed (via parse(" + Double.toString(d2) + " != " + Double.toString(d) + ")) for " + text);
}
logln(text + " parsed into " + (long) d2);
// ======= Test getters and setters
logln("Testing getters and setters");
final DecimalFormatSymbols syms = pat.getDecimalFormatSymbols();
def.setDecimalFormatSymbols(syms);
if (!pat.getDecimalFormatSymbols().equals(def.getDecimalFormatSymbols())) {
errln("ERROR: set DecimalFormatSymbols() failed");
}
String posPrefix;
pat.setPositivePrefix("+");
posPrefix = pat.getPositivePrefix();
logln("Positive prefix (should be +): " + posPrefix);
if (posPrefix != "+") {
errln("ERROR: setPositivePrefix() failed");
}
String negPrefix;
pat.setNegativePrefix("-");
negPrefix = pat.getNegativePrefix();
logln("Negative prefix (should be -): " + negPrefix);
if (negPrefix != "-") {
errln("ERROR: setNegativePrefix() failed");
}
String posSuffix;
pat.setPositiveSuffix("_");
posSuffix = pat.getPositiveSuffix();
logln("Positive suffix (should be _): " + posSuffix);
if (posSuffix != "_") {
errln("ERROR: setPositiveSuffix() failed");
}
String negSuffix;
pat.setNegativeSuffix("~");
negSuffix = pat.getNegativeSuffix();
logln("Negative suffix (should be ~): " + negSuffix);
if (negSuffix != "~") {
errln("ERROR: setNegativeSuffix() failed");
}
long multiplier = 0;
pat.setMultiplier(8);
multiplier = pat.getMultiplier();
logln("Multiplier (should be 8): " + multiplier);
if (multiplier != 8) {
errln("ERROR: setMultiplier() failed");
}
int groupingSize = 0;
pat.setGroupingSize(2);
groupingSize = pat.getGroupingSize();
logln("Grouping size (should be 2): " + (long) groupingSize);
if (groupingSize != 2) {
errln("ERROR: setGroupingSize() failed");
}
pat.setDecimalSeparatorAlwaysShown(true);
boolean tf = pat.isDecimalSeparatorAlwaysShown();
logln("DecimalSeparatorIsAlwaysShown (should be true) is " + (tf ? "true" : "false"));
if (tf != true) {
errln("ERROR: setDecimalSeparatorAlwaysShown() failed");
}
String funkyPat;
funkyPat = pat.toPattern();
logln("Pattern is " + funkyPat);
String locPat;
locPat = pat.toLocalizedPattern();
logln("Localized pattern is " + locPat);
pat.setCurrencyPluralInfo(infoInput);
if (!infoInput.equals(pat.getCurrencyPluralInfo())) {
errln("ERROR: set/get CurrencyPluralInfo() failed");
}
pat.setCurrencyPluralInfo(infoInput);
if (!infoInput.equals(pat.getCurrencyPluralInfo())) {
errln("ERROR: set/get CurrencyPluralInfo() failed");
}
// ======= Test applyPattern()
logln("Testing applyPattern()");
String p1 = new String("#,##0.0#;(#,##0.0#)");
logln("Applying pattern " + p1);
pat.applyPattern(p1);
String s2;
s2 = pat.toPattern();
logln("Extracted pattern is " + s2);
if (!s2.equals(p1)) {
errln("ERROR: toPattern() result did not match pattern applied");
}
String p2 = new String("#,##0.0# FF;(#,##0.0# FF)");
logln("Applying pattern " + p2);
pat.applyLocalizedPattern(p2);
String s3;
s3 = pat.toLocalizedPattern();
logln("Extracted pattern is " + s3);
if (!s3.equals(p2)) {
errln("ERROR: toLocalizedPattern() result did not match pattern applied");
}
// ======= Test getStaticClassID()
// logln("Testing instanceof()");
// try {
// NumberFormat test = new DecimalFormat();
// if (! (test instanceof DecimalFormat)) {
// errln("ERROR: instanceof failed");
// }
// }
// catch (Exception e) {
// errln("ERROR: Couldn't create a DecimalFormat");
// }
}
use of android.icu.text.DecimalFormat in project j2objc by google.
the class IntlTestDecimalFormatSymbolsC method verify.
/**
* helper functions*
*/
public void verify(double value, String pattern, DecimalFormatSymbols sym, String expected) {
DecimalFormat df = new DecimalFormat(pattern, sym);
StringBuffer buffer = new StringBuffer("");
FieldPosition pos = new FieldPosition(-1);
buffer = df.format(value, buffer, pos);
if (!buffer.toString().equals(expected)) {
errln("ERROR: format failed after setSymbols()\n Expected" + expected + ", Got " + buffer);
}
}
Aggregations