List of usage examples for java.text DecimalFormatSymbols getInstance
public static final DecimalFormatSymbols getInstance(Locale locale)
From source file:org.openestate.io.core.NumberUtils.java
/** * Test, if a string contains a parsable number. * * @param value/*from ww w . j av a 2 s. c o m*/ * the value to check * * @param locale * the locale, against which the value is checked * (checks locale specific decimal and grouping separators) * * @return * true, if the provided value contains of numbers */ public static boolean isNumeric(String value, Locale locale) { if (value == null) return false; int start = 0; final DecimalFormatSymbols symbols = (locale != null) ? DecimalFormatSymbols.getInstance(locale) : DecimalFormatSymbols.getInstance(); if (value.startsWith("+") || value.startsWith("-")) start++; boolean fraction = false; for (int i = start; i < value.length(); i++) { final char c = value.charAt(i); if (c == symbols.getDecimalSeparator() && !fraction) { fraction = true; continue; } //if (c==symbols.getGroupingSeparator() && !fraction) //{ // continue; //} if (!Character.isDigit(c)) { return false; } } return true; }
From source file:ai.grakn.graql.internal.util.StringConverter.java
/** * @param value a value in the graph/*from w w w.java 2s . com*/ * @return the string representation of the value (using quotes if it is already a string) */ public static String valueToString(Object value) { if (value instanceof String) { return quoteString((String) value); } else if (value instanceof Double) { DecimalFormat df = new DecimalFormat("#", DecimalFormatSymbols.getInstance(Locale.ENGLISH)); df.setMinimumFractionDigits(1); df.setMaximumFractionDigits(12); df.setMinimumIntegerDigits(1); return df.format(value); } else { return value.toString(); } }
From source file:MondrianConnector.java
public ArrayList<LinkedHashMap<String, String>> ExecuteQuery(Query queryObject) throws Exception { System.setProperty("mondrian.olap.SsasCompatibleNaming", "true"); String connectionString = getConnectionString(queryObject); RolapConnection connection = (RolapConnection) DriverManager.getConnection(connectionString, null); mondrian.olap.Query query = connection.parseQuery(queryObject.getMdxQuery()); Result result = connection.execute(query); ArrayList<LinkedHashMap<String, String>> data = new ArrayList<LinkedHashMap<String, String>>(); DecimalFormat df = new DecimalFormat("0", DecimalFormatSymbols.getInstance(Locale.ENGLISH)); df.setMaximumFractionDigits(340); //340 = DecimalFormat.DOUBLE_FRACTION_DIGITS if (result.getAxes().length == 1) { //Only One Axis has come so ArrayList<String> measures = new ArrayList<String>(); for (Position p : result.getAxes()[0].getPositions()) { measures.add(p.get(0).getUniqueName().toString()); }//from w ww. ja va 2s. c o m LinkedHashMap<String, String> row = new LinkedHashMap<String, String>(); for (int i = 0; i < measures.size(); i++) { Object value = result.getCell(new int[] { i }).getValue(); if (value == null) { row.put(measures.get(i), null); } else if (value instanceof Integer) { row.put(measures.get(i), ((Integer) value).toString()); } else if (value instanceof Double) { row.put(measures.get(i), df.format(value)); } else { row.put(measures.get(i), value.toString()); } } data.add(row); } else if (result.getAxes().length == 2) { ArrayList<String> measures = new ArrayList<String>(); for (Position p : result.getAxes()[0].getPositions()) { measures.add(p.get(0).getUniqueName().toString()); } ArrayList<ArrayList<DimensionItem>> dimensionItems = new ArrayList<ArrayList<DimensionItem>>(); for (Position p : result.getAxes()[1].getPositions()) { ArrayList<DimensionItem> itemsAtRow = new ArrayList<DimensionItem>(); for (Object item : p.toArray()) { RolapMemberBase member = (RolapMemberBase) item; itemsAtRow.add(new DimensionItem(member.getLevel().getHierarchy().toString(), member.getCaption().toString())); } dimensionItems.add(itemsAtRow); } for (int ix = 0; ix < dimensionItems.size(); ix++) { LinkedHashMap<String, String> row = new LinkedHashMap<String, String>(); for (DimensionItem item : dimensionItems.get(ix)) { row.put(item.getLevel(), item.getCaption()); } for (int i = 0; i < measures.size(); i++) { Object value = result.getCell(new int[] { i, ix }).getValue(); if (value == null) { row.put(measures.get(i), "0"); } else { if (value instanceof Integer) { row.put(measures.get(i), ((Integer) value).toString()); } else if (value instanceof Double) { row.put(measures.get(i), df.format(value)); } else { row.put(measures.get(i), value.toString()); } } } data.add(row); } } return data; }
From source file:org.structr.core.parser.function.NumberFormatFunction.java
@Override public Object apply(final ActionContext ctx, final GraphObject entity, final Object[] sources) throws FrameworkException { if (sources == null || sources != null && sources.length != 3) { return usage(ctx.isJavaScriptContext()); }//w w w . j av a 2 s . co m if (arrayHasLengthAndAllElementsNotNull(sources, 3)) { if (StringUtils.isBlank(sources[0].toString())) { return ""; } try { Double val = Double.parseDouble(sources[0].toString()); String langCode = sources[1].toString(); String pattern = sources[2].toString(); return new DecimalFormat(pattern, DecimalFormatSymbols.getInstance(Locale.forLanguageTag(langCode))) .format(val); } catch (Throwable t) { } } return ""; }
From source file:org.structr.common.LogCommandsTest.java
public void test01TestSequentialWriteRead() { try {/* ww w .j a va 2 s. co m*/ int number = 100; String logPageKey = "test1"; long t0 = System.nanoTime(); for (int i = 0; i < number; i++) { writeLogCommand.execute(logPageKey, new String[] { "foo" + i, "bar" }); } long t1 = System.nanoTime(); DecimalFormat decimalFormat = new DecimalFormat("0.000000000", DecimalFormatSymbols.getInstance(Locale.ENGLISH)); Double time = (t1 - t0) / 1000000000.0; Double rate = number / ((t1 - t0) / 1000000000.0); logger.log(Level.INFO, "Created {0} log entries in {1} seconds ({2} per s)", new Object[] { number, decimalFormat.format(time), decimalFormat.format(rate) }); Map<String, Object> result = (Map<String, Object>) readLogCommand.execute(logPageKey); for (Entry<String, Object> entry : result.entrySet()) { String key = entry.getKey(); Object val = entry.getValue(); assertTrue(val instanceof String[]); String[] values = (String[]) val; // System.out.println(key + ": " + StringUtils.join(values, ",")); } assertEquals(number, result.size()); for (int i = 0; i < number; i++) { writeLogCommand.execute(logPageKey, new String[] { "foo" + i, "bar" }); } result = (Map<String, Object>) readLogCommand.execute(logPageKey); assertTrue(result.size() == 2 * number); } catch (FrameworkException ex) { logger.log(Level.SEVERE, ex.toString()); fail("Unexpected exception"); } }
From source file:org.echocat.locela.api.java.format.NumberFormatter.java
@Nonnull protected FormatAndPattern evaluate(@Nonnull Locale locale, @Nullable String pattern) { final FormatAndPattern result; if (isEmpty(pattern)) { result = new FormatAndPattern(DEFAULT.toFormat(locale), DEFAULT.getId()); } else {/*from w ww . ja va 2s . c o m*/ final Pattern predefined = Pattern.NAME_TO_PATTERN.get(pattern); if (predefined != null) { result = new FormatAndPattern(predefined.toFormat(locale), predefined.getId()); } else { result = new FormatAndPattern(new DecimalFormat(pattern, DecimalFormatSymbols.getInstance(locale)), pattern); } } return result; }
From source file:tagtime.beeminder.BeeminderGraph.java
/** * Parses a graph's data and sets up a .bee file to track which tags * have been submitted to the graph./*w w w . ja v a 2 s . c om*/ * @param username The user's Beeminder username. * @param dataEntry The data entry for the current graph. This must * be in the format "graphName|tags". */ public BeeminderGraph(TagTime tagTimeInstance, String username, String dataEntry) { if (username == null || dataEntry == null) { throw new IllegalArgumentException( "Both parameters to the " + "BeeminderGraphData constructor must be defined."); } this.tagTimeInstance = tagTimeInstance; int decimalDigits = tagTimeInstance.settings.getIntValue(SettingType.PRECISION); hourFormatter = new DecimalFormat(); hourFormatter.setGroupingUsed(false); hourFormatter.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.US)); hourFormatter.setRoundingMode(RoundingMode.HALF_UP); hourFormatter.setMaximumFractionDigits(decimalDigits); roundingMultiplier = (int) Math.pow(10, decimalDigits); //find the graph name int graphDelim = dataEntry.indexOf('|'); if (graphDelim < 0 || graphDelim >= dataEntry.length() - 1) { throw new IllegalArgumentException("dataEntry must be in " + "format \"graphname|tags\""); } graphName = dataEntry.substring(0, graphDelim); //get the tags String[] tags = dataEntry.substring(graphDelim + 1).split("\\s+"); List<String> acceptedTags = new ArrayList<String>(3); List<String> rejectedTags = new ArrayList<String>(0); //enter the tags in the correct lists for (String tag : tags) { if (tag.charAt(0) == '-') { rejectedTags.add(tag.substring(1).toLowerCase()); } else { acceptedTags.add(tag.toLowerCase()); } } //make sure some tags were entered if (acceptedTags.size() == 0 && rejectedTags.size() == 0) { throw new IllegalArgumentException("No tags provided."); } tagMatcher = new TagMatcher(acceptedTags, rejectedTags); }
From source file:org.insightedge.TestCluster.java
private static String oneDigit(double value) { return new DecimalFormat("#0.0", DecimalFormatSymbols.getInstance(Locale.US)).format(value); }
From source file:org.structr.common.LogCommandsTest.java
public void test02TestUnknownPageKey() { try {//ww w . j a v a 2 s .c o m int number = 100; String logPageKey = "test1"; long t0 = System.nanoTime(); for (int i = 0; i < number; i++) { writeLogCommand.execute(logPageKey, new String[] { "foo" + i, "bar" }); } long t1 = System.nanoTime(); DecimalFormat decimalFormat = new DecimalFormat("0.000000000", DecimalFormatSymbols.getInstance(Locale.ENGLISH)); Double time = (t1 - t0) / 1000000000.0; Double rate = number / ((t1 - t0) / 1000000000.0); logger.log(Level.INFO, "Created {0} log entries in {1} seconds ({2} per s)", new Object[] { number, decimalFormat.format(time), decimalFormat.format(rate) }); logPageKey = "test2"; Map<String, Object> result = (Map<String, Object>) readLogCommand.execute(logPageKey); assertTrue(result.isEmpty()); } catch (FrameworkException ex) { logger.log(Level.SEVERE, ex.toString()); fail("Unexpected exception"); } }
From source file:net.sf.jasperreports.swing.JRViewerToolbar.java
public JRViewerToolbar(JRViewerController viewerContext) { this.viewerContext = viewerContext; this.viewerContext.addListener(this); zoomDecimalFormat = new DecimalFormat("#.##", DecimalFormatSymbols.getInstance(viewerContext.getLocale())); initComponents();/*w w w .j av a 2 s .c o m*/ initSaveContributors(); }