下面列出了java.text.DecimalFormatSymbols#getInstance ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
*
*/
protected void initResources(Locale locale, ResourceBundle resBundle)
{
//FIXME in theory, the setLocale method could be called after current Component was created, in which case all below should be reloaded
if (locale != null)
{
setLocale(locale);
}
else
{
setLocale(Locale.getDefault());
}
if (resBundle == null)
{
this.resourceBundle = ResourceBundle.getBundle("net/sf/jasperreports/view/viewer", getLocale());
}
else
{
this.resourceBundle = resBundle;
}
zoomDecimalFormat = new DecimalFormat("#.##", DecimalFormatSymbols.getInstance(getLocale()));
}
public void testParseFloatNonRootLocale() throws Exception {
final DecimalFormatSymbols fr_FR = DecimalFormatSymbols.getInstance(new Locale("fr","FR"));
final char groupChar = fr_FR.getGroupingSeparator();
final char decimalChar = fr_FR.getDecimalSeparator();
float value = 10898.83491F;
String floatString1 = "10898"+decimalChar+"83491";
String floatString2 = "10"+groupChar+"898"+decimalChar+"83491";
IndexSchema schema = h.getCore().getLatestSchema();
assertNotNull(schema.getFieldOrNull("float_f")); // should match dynamic field "*_f"
assertNull(schema.getFieldOrNull("not_in_schema"));
SolrInputDocument d = processAdd("parse-float-french-no-run-processor",
doc(f("id", "140"), f("float_f", floatString1),
f("not_in_schema", floatString2)));
assertNotNull(d);
assertThat(d.getFieldValue("float_f"), IS_FLOAT);
assertEquals(value, (Float)d.getFieldValue("float_f"), EPSILON);
assertThat(d.getFieldValue("not_in_schema"), IS_FLOAT);
assertEquals(value, (Float)d.getFieldValue("not_in_schema"), EPSILON);
}
@Test
public void testLocaleen_US() {
final DecimalFormatSymbols theSymbols = DecimalFormatSymbols.getInstance(new Locale("en","US"));
Assert.assertEquals("0", toString(theSymbols.getZeroDigit()));
Assert.assertEquals(",", toString(theSymbols.getGroupingSeparator()));
Assert.assertEquals(".", toString(theSymbols.getDecimalSeparator()));
//Assert.assertEquals("‰", toString(theSymbols.getPerMill()));
Assert.assertEquals("%", toString(theSymbols.getPercent()));
Assert.assertEquals("#", toString(theSymbols.getDigit()));
Assert.assertEquals(";", toString(theSymbols.getPatternSeparator()));
//Assert.assertEquals("∞", theSymbols.getInfinity());
//Assert.assertEquals("NaN", theSymbols.getNaN());
Assert.assertEquals("-", toString(theSymbols.getMinusSign()));
Assert.assertEquals("$", theSymbols.getCurrencySymbol());
Assert.assertEquals("USD", theSymbols.getInternationalCurrencySymbol());
Assert.assertEquals(".", toString(theSymbols.getMonetaryDecimalSeparator()));
Assert.assertEquals("E", theSymbols.getExponentSeparator());
}
/**
* Formats the double flow-point number.
*
* @param number The number to format.
* @return formatted flow-point.
*/
private String formatNumber(double number) {
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
symbols.setDecimalSeparator('.');
DecimalFormat format = new DecimalFormat("#.##########", symbols);
return format.format(number);
}
@Test
public void testCustomValueSeparatorWithEL() throws Exception {
Map<String, String> attributes = new HashMap<String, String>();
attributes.put("csv.delimiter", "|");
testRunner.enqueue(new File("src/test/resources/dataformatting.xlsx").toPath(), attributes);
testRunner.setProperty(CSVUtils.VALUE_SEPARATOR, "${csv.delimiter}");
testRunner.setProperty(ConvertExcelToCSVProcessor.FORMAT_VALUES, "true");
testRunner.run();
testRunner.assertTransferCount(ConvertExcelToCSVProcessor.SUCCESS, 1);
testRunner.assertTransferCount(ConvertExcelToCSVProcessor.ORIGINAL, 1);
testRunner.assertTransferCount(ConvertExcelToCSVProcessor.FAILURE, 0);
MockFlowFile ff = testRunner.getFlowFilesForRelationship(ConvertExcelToCSVProcessor.SUCCESS).get(0);
Long rowsSheet = new Long(ff.getAttribute(ConvertExcelToCSVProcessor.ROW_NUM));
assertTrue(rowsSheet == 9);
LocalDateTime localDt = LocalDateTime.of(2017, 1, 1, 12, 0, 0);
DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance();
String valueSeparator = testRunner.getProcessContext().getProperty(CSVUtils.VALUE_SEPARATOR).evaluateAttributeExpressions(ff).getValue();
String decimalSeparator = (String.valueOf(decimalFormatSymbols.getDecimalSeparator()).equals(valueSeparator))
? ("\\" + decimalFormatSymbols.getDecimalSeparator()) : String.valueOf(decimalFormatSymbols.getDecimalSeparator());
String groupingSeparator = String.valueOf(decimalFormatSymbols.getGroupingSeparator()).equals(valueSeparator)
? "\\" + decimalFormatSymbols.getGroupingSeparator() : String.valueOf(decimalFormatSymbols.getGroupingSeparator());
ff.assertContentEquals(String.format("Numbers|Timestamps|Money\n" +
"1234%1$s456|" + DateTimeFormatter.ofPattern("d/M/yy").format(localDt) + "|$ 123%1$s45\n" +
"1234%1$s46|" + DateTimeFormatter.ofPattern("hh:mm:ss a").format(localDt) + "|£ 123%1$s45\n" +
"1234%1$s5|" + DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy").format(localDt) + "|¥ 123%1$s45\n" +
"1%2$s234%1$s46|" + DateTimeFormatter.ofPattern("d/M/yy HH:mm").format(localDt) + "|$ 1%2$s023%1$s45\n" +
"1%2$s234%1$s4560|" + DateTimeFormatter.ofPattern("hh:mm a").format(localDt) + "|£ 1%2$s023%1$s45\n" +
"9%1$s88E+08|" + DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm").format(localDt) + "|¥ 1%2$s023%1$s45\n" +
"9%1$s877E+08||\n" +
"9%1$s8765E+08||\n", decimalSeparator, groupingSeparator));
}
private static char getZero(Locale l) {
if ((l != null) && !l.equals(Locale.US)) {
DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(l);
return dfs.getZeroDigit();
} else {
return '0';
}
}
private static DecimalStyle create(Locale locale) {
DecimalFormatSymbols oldSymbols = DecimalFormatSymbols.getInstance(locale);
char zeroDigit = oldSymbols.getZeroDigit();
char positiveSign = '+';
char negativeSign = oldSymbols.getMinusSign();
char decimalSeparator = oldSymbols.getDecimalSeparator();
if (zeroDigit == '0' && negativeSign == '-' && decimalSeparator == '.') {
return STANDARD;
}
return new DecimalStyle(zeroDigit, positiveSign, negativeSign, decimalSeparator);
}
private int countDecimalSeparator(CharSequence strValue) {
DecimalFormatSymbols d = DecimalFormatSymbols.getInstance(Locale.getDefault());
int posStart = String.valueOf(strValue).indexOf(d.getDecimalSeparator());
if (posStart > -1) {
int posEnd = String.valueOf(strValue).lastIndexOf(d.getDecimalSeparator());
return posStart == posEnd ? 1 : 2;
}
return 0;
}
private static char getZero(Locale l) {
if ((l != null) && !l.equals(Locale.US)) {
DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(l);
return dfs.getZeroDigit();
} else {
return '0';
}
}
/**
* 数値のフォーマッタを作成する。
* <p>アノテーション{@link CsvNumberFormat}の値を元に作成します。</p>
* @param field フィールド情報
* @param config システム設定
* @return アノテーション{@link CsvNumberFormat}が付与されていない場合は、空を返す。
*/
@SuppressWarnings("unchecked")
protected Optional<NumberFormatWrapper<N>> createFormatter(final FieldAccessor field, final Configuration config) {
final Optional<CsvNumberFormat> formatAnno = field.getAnnotation(CsvNumberFormat.class);
if(!formatAnno.isPresent()) {
return Optional.empty();
}
final String pattern = formatAnno.get().pattern();
if(pattern.isEmpty()) {
return Optional.empty();
}
final boolean lenient = formatAnno.get().lenient();
final Locale locale = Utils.getLocale(formatAnno.get().locale());
final Optional<Currency> currency = formatAnno.get().currency().isEmpty() ? Optional.empty()
: Optional.of(Currency.getInstance(formatAnno.get().currency()));
final RoundingMode roundingMode = formatAnno.get().rounding();
final DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
final DecimalFormat formatter = new DecimalFormat(pattern, symbols);
formatter.setParseBigDecimal(true);
formatter.setRoundingMode(roundingMode);
currency.ifPresent(c -> formatter.setCurrency(c));
final NumberFormatWrapper<N> wrapper = new NumberFormatWrapper<>(formatter, (Class<N>)field.getType(), lenient);
wrapper.setValidationMessage(formatAnno.get().message());
return Optional.of(wrapper);
}
private char getZero(Locale l) {
if ((l != null) && !l.equals(locale())) {
DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(l);
return dfs.getZeroDigit();
}
return zero;
}
@Test
public void testParsePattern() {
Locale.setDefault(Locale.GERMANY);
DecimalFormatSymbols instance = DecimalFormatSymbols.getInstance();
Location location = Location.of(Latitude.ofDegrees(23.987635));
LocationFormatter locationFormatter = LocationFormatter.builder()
.appendPattern("DD.DD")
.build();
Assert.assertEquals(instance.getDecimalSeparator(), ',');
Assert.assertEquals(locationFormatter.toPattern(), "DD.DD");
Assert.assertEquals(locationFormatter.format(location), "23.99");
}
private void updateFee() {
BigInteger defaultGasLimit = BigInteger.valueOf(DEFAULT_GAS_LIMIT);
BigInteger fee = BigInteger.ZERO;
if (gasPrice != null) {
fee = gasPrice.multiply(defaultGasLimit);
}
currentFeeEth = Convert.fromWei(new BigDecimal(fee), Convert.Unit.ETHER);
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
symbols.setDecimalSeparator('.');
DecimalFormat decimalFormat = new DecimalFormat(ETH_SHOW_PATTERN, symbols);
String feeStr = decimalFormat.format(currentFeeEth);
etFeeAmount.setText(feeStr);
updateArrivalField();
}
private static DecimalStyle create(Locale locale) {
DecimalFormatSymbols oldSymbols = DecimalFormatSymbols.getInstance(locale);
char zeroDigit = oldSymbols.getZeroDigit();
char positiveSign = '+';
char negativeSign = oldSymbols.getMinusSign();
char decimalSeparator = oldSymbols.getDecimalSeparator();
if (zeroDigit == '0' && negativeSign == '-' && decimalSeparator == '.') {
return STANDARD;
}
return new DecimalStyle(zeroDigit, positiveSign, negativeSign, decimalSeparator);
}
public TickUnitSource createIntegerTickUnits(Locale locale)
{
DecimalFormatSymbols formatSymbols = DecimalFormatSymbols.getInstance(locale);
// copied from NumberAxis.createIntegerTickUnits() to preserve backward behaviour
TickUnits units = new TickUnits();
DecimalFormat df0 = new DecimalFormat("0", formatSymbols);
DecimalFormat df1 = new DecimalFormat("#,##0", formatSymbols);
units.add(new NumberTickUnit(1, df0));
units.add(new NumberTickUnit(2, df0));
units.add(new NumberTickUnit(5, df0));
units.add(new NumberTickUnit(10, df0));
units.add(new NumberTickUnit(20, df0));
units.add(new NumberTickUnit(50, df0));
units.add(new NumberTickUnit(100, df0));
units.add(new NumberTickUnit(200, df0));
units.add(new NumberTickUnit(500, df0));
units.add(new NumberTickUnit(1000, df1));
units.add(new NumberTickUnit(2000, df1));
units.add(new NumberTickUnit(5000, df1));
units.add(new NumberTickUnit(10000, df1));
units.add(new NumberTickUnit(20000, df1));
units.add(new NumberTickUnit(50000, df1));
units.add(new NumberTickUnit(100000, df1));
units.add(new NumberTickUnit(200000, df1));
units.add(new NumberTickUnit(500000, df1));
units.add(new NumberTickUnit(1000000, df1));
units.add(new NumberTickUnit(2000000, df1));
units.add(new NumberTickUnit(5000000, df1));
units.add(new NumberTickUnit(10000000, df1));
units.add(new NumberTickUnit(20000000, df1));
units.add(new NumberTickUnit(50000000, df1));
units.add(new NumberTickUnit(100000000, df1));
units.add(new NumberTickUnit(200000000, df1));
units.add(new NumberTickUnit(500000000, df1));
units.add(new NumberTickUnit(1000000000, df1));
units.add(new NumberTickUnit(2000000000, df1));
units.add(new NumberTickUnit(5000000000.0, df1));
units.add(new NumberTickUnit(10000000000.0, df1));
// adding further values by default because 1E10 is not enough for some people
// using getNumberInstance because that's what NumberAxis.createIntegerTickUnits does
units.add(new NumberTickUnit(20000000000L, df1));
units.add(new NumberTickUnit(50000000000L, df1));
units.add(new NumberTickUnit(100000000000L, df1));
units.add(new NumberTickUnit(200000000000L, df1));
units.add(new NumberTickUnit(500000000000L, df1));
units.add(new NumberTickUnit(1000000000000L, df1));
units.add(new NumberTickUnit(2000000000000L, df1));
units.add(new NumberTickUnit(5000000000000L, df1));
units.add(new NumberTickUnit(10000000000000L, df1));
units.add(new NumberTickUnit(20000000000000L, df1));
units.add(new NumberTickUnit(50000000000000L, df1));
units.add(new NumberTickUnit(100000000000000L, df1));
units.add(new NumberTickUnit(200000000000000L, df1));
units.add(new NumberTickUnit(500000000000000L, df1));
units.add(new NumberTickUnit(1000000000000000L, df1));
units.add(new NumberTickUnit(2000000000000000L, df1));
units.add(new NumberTickUnit(5000000000000000L, df1));
units.add(new NumberTickUnit(10000000000000000L, df1));
units.add(new NumberTickUnit(20000000000000000L, df1));
units.add(new NumberTickUnit(50000000000000000L, df1));
units.add(new NumberTickUnit(100000000000000000L, df1));
units.add(new NumberTickUnit(200000000000000000L, df1));
units.add(new NumberTickUnit(500000000000000000L, df1));
units.add(new NumberTickUnit(1000000000000000000L, df1));
units.add(new NumberTickUnit(2000000000000000000L, df1));
units.add(new NumberTickUnit(5000000000000000000L, df1));
return units;
}
private void checkDigit(Locale loc, Character expected) {
DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(loc);
Character zero = dfs.getZeroDigit();
assertEquals("Wrong digit zero char", expected, zero);
}
private void checkDigit(Locale loc, Character expected) {
DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(loc);
Character zero = dfs.getZeroDigit();
assertEquals("Wrong digit zero char", expected, zero);
}
public String formatAsHoursMinutes(int hours, int minutes) {
DecimalFormat df = new DecimalFormat("00", DecimalFormatSymbols.getInstance(Locale.getDefault()));
return hours + ":" + df.format(minutes);
}
private String formatCalibrations() {
// MongoDB Index Key Limit
// The total size of an index entry, which can include structural overhead depending on the BSON type,
// must be less than 1024 bytes.
// This is enough space for around 17 formatted data points
// When there are more then this, a log function is used to select points preferring the most recent
int style = 1;
int segMin = 10;
int segMax = 17;
double fontMin = 50;
double fontMax = 65;
int height = 60;
double border = 0.5;
double segWidth;
String css;
if (style == 1) {
segWidth = 6;
fontMin = 55;
fontMax = 68;
css = ".wr{height:%spx;position:relative;border:1px solid#aaa;background-color:#fff;text-align:center}" +
".wr div{width:%s%%;position:absolute;background-color:#bbb;color:#000;height:2px}";
} else if (style == 2) {
segWidth = 8;
fontMin = 55;
fontMax = 70;
css = ".wr{height:%spx;position:relative;border:1px solid#aaa;background-color:#fff;text-align:center}" +
".wr div{width:%s%%;position:absolute;background-color:#aaa2;color:#000;line-height:120%%;border-radius:10px}";
} else {
segWidth = 7;
css = ".wr{height:%spx;position:relative;border:1px solid#aaa;background-color:#fff;text-align:center}" +
".wr div{width:%s%%;position:absolute;background-color:#ddd;color:#000;bottom:0}";
}
int total = calibrations.length;
int seg = total < segMin ? segMin : (total > segMax ? segMax : total);
int count = total < seg ? total : seg;
double xadj = ((((100.0 - (segMin * segWidth)) / segMin) / 2) / (segMax - segMin)) * (segMax - seg);
double ymax = 100 - (border * 2);
double xmax = 100 - ((border + xadj) * 2);
double xpos = border + xadj;
double xadd = (xmax - segWidth) / (seg - 1);
double font = fontMax - (((fontMax - fontMin) / (segMax - segMin)) * (seg - segMin));
double scale = 1.5;
double smid = 50;
double vmin = 22;
double vmax = 97;
double vmid = 42;
DecimalFormat dfFacLocal = new DecimalFormat("0", DecimalFormatSymbols.getInstance(Locale.getDefault()));
dfFacLocal.setMinimumFractionDigits(0);
dfFacLocal.setMaximumFractionDigits(1);
DecimalFormat dfFac = new DecimalFormat("0", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
dfFac.setMinimumFractionDigits(0);
dfFac.setMaximumFractionDigits(1);
DecimalFormat dfNum = new DecimalFormat("0");
dfNum.setMinimumFractionDigits(0);
dfNum.setMaximumFractionDigits(0);
StringBuilder parts = new StringBuilder();
double v, r, l, p;
for (int i = 0; i < count; i++) {
l = Math.log10((i + 1) * (100 / seg));
p = i + ((l * (total - count)) / 2);
v = calibrations[(int) Math.round(p)] & 0xFF; // cal factor as 8bit div 10
if (v > 100) v = 10 * (int) (v / 10); // keep cal factors > 10 as integers due to html space limitations
r = vmid + ((v - smid) * scale);
r = r < vmin ? vmin : (r > vmax ? vmax : r);
parts.append(String.format("<div style='left:%s%%;top:%s%%'>%s</div>",
dfFac.format(xpos),
dfNum.format(100 - ((ymax * r) / 100)),
dfFacLocal.format(v / 10)));
xpos += xadd;
}
return String.format("<style>" + css + "</style><div class='wr'style='font-size:%s%%'>%s</div>",
dfNum.format(height),
dfFac.format(segWidth),
dfNum.format(font),
parts.toString());
}
/**
* Creates a new instance of {@link MonetaryAmountDecimalFormatBuilder} with {@link Locale} set from parameter and pattern to format the {@link javax.money.MonetaryAmount}.
* @param pattern the pattern to be used
* @param locale the target locale
* @see DecimalFormat
* @see DecimalFormat#DecimalFormat(String)
* @return a new instance of {@link MonetaryAmountDecimalFormatBuilder}
*/
public static MonetaryAmountDecimalFormatBuilder of(String pattern, Locale locale) {
MonetaryAmountDecimalFormatBuilder builder = new MonetaryAmountDecimalFormatBuilder();
builder.decimalFormat = new DecimalFormat(pattern, DecimalFormatSymbols.getInstance(locale));
builder.locale = locale;
return builder;
}