List of usage examples for java.text NumberFormat setMaximumFractionDigits
public void setMaximumFractionDigits(int newValue)
From source file:org.kuali.ole.sys.context.Log4jConfigurer.java
protected static NumberFormat getNumberFormatter() { NumberFormat nf = NumberFormat.getInstance(); nf.setGroupingUsed(false);/*from w w w. ja va2 s. co m*/ nf.setMaximumFractionDigits(1); nf.setMinimumFractionDigits(1); return nf; }
From source file:org.techytax.helper.AmountHelper.java
public static String formatDecimal(BigInteger b) { Locale loc = new Locale("nl", "NL", "EURO"); NumberFormat n = NumberFormat.getCurrencyInstance(loc); double doublePayment = b.doubleValue(); n.setMaximumFractionDigits(0); String s = n.format(doublePayment); return s;//from w ww.java 2 s . c o m }
From source file:Main.java
public static String sGetDecimalStringAnyLocaleAs1Pt5LocalisedString(String value) { Locale theLocale = Locale.getDefault(); NumberFormat numberFormat = DecimalFormat.getInstance(theLocale); Number theNumber;//from ww w . j a va 2 s . c o m try { theNumber = numberFormat.parse(value); } catch (ParseException e) { //value = sConvertDigits(value); String valueWithDot = value.replaceAll(",", "."); theNumber = Double.valueOf(valueWithDot); } // String.format uses the JVM's default locale. //String s = String.format(Locale.US, "%.2f", price); NumberFormat outputFormat = DecimalFormat.getInstance(theLocale); outputFormat.setMinimumIntegerDigits(1); outputFormat.setMinimumFractionDigits(5); outputFormat.setMaximumFractionDigits(5); String theStringResult = outputFormat.format(theNumber); //String theStringResult = String.format("%1.5f", theDoubleValue.doubleValue()); return theStringResult; }
From source file:WordCountSplitTest.java
private final static void test(boolean use_shards, boolean use_chunks, Boolean slaveok) throws Exception { did_start = false;/*from ww w . ja v a 2 s . co m*/ final Configuration conf = new Configuration(); MongoConfigUtil.setInputURI(conf, "mongodb://localhost:30000/test.lines"); conf.setBoolean(MongoConfigUtil.SPLITS_USE_SHARDS, use_shards); conf.setBoolean(MongoConfigUtil.SPLITS_USE_CHUNKS, use_chunks); String output_table = null; if (use_chunks) { if (use_shards) output_table = "with_shards_and_chunks"; else output_table = "with_chunks"; } else { if (use_shards) output_table = "with_shards"; else output_table = "no_splits"; } if (slaveok != null) { output_table += "_" + slaveok; } MongoConfigUtil.setOutputURI(conf, "mongodb://localhost:30000/test." + output_table); System.out.println("Conf: " + conf); final Job job = new Job(conf, "word count " + output_table); job.setJarByClass(WordCountSplitTest.class); job.setMapperClass(TokenizerMapper.class); job.setCombinerClass(IntSumReducer.class); job.setReducerClass(IntSumReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); job.setInputFormatClass(MongoInputFormat.class); job.setOutputFormatClass(MongoOutputFormat.class); final long start = System.currentTimeMillis(); System.out.println(" ----------------------- running test " + output_table + " --------------------"); try { boolean result = job.waitForCompletion(true); System.out.println("job.waitForCompletion( true ) returned " + result); } catch (Exception e) { System.out.println("job.waitForCompletion( true ) threw Exception"); e.printStackTrace(); } final long end = System.currentTimeMillis(); final float seconds = ((float) (end - start)) / 1000; java.text.NumberFormat nf = java.text.NumberFormat.getInstance(); nf.setMaximumFractionDigits(3); System.out.println("finished run in " + nf.format(seconds) + " seconds"); com.mongodb.Mongo m = new com.mongodb.Mongo( new com.mongodb.MongoURI("mongodb://localhost:30000/?slaveok=true")); com.mongodb.DB db = m.getDB("test"); com.mongodb.DBCollection coll = db.getCollection(output_table); com.mongodb.BasicDBObject query = new com.mongodb.BasicDBObject(); query.put("_id", "the"); com.mongodb.DBCursor cur = coll.find(query); if (!cur.hasNext()) System.out.println("FAILURE: could not find count of \'the\'"); else System.out.println("'the' count: " + cur.next()); // if (! result) // System.exit( 1 ); }
From source file:siddur.solidtrust.cost.CarCostService.java
private static String tranfer(String value) { //Marktplaats is based on 1250 kilometers per month. I want to recalculate this for 1083 kilometers float rate = 1083f / 1250f; String v = value.replace(",", "."); if (StringUtils.isNumeric(v)) { float rc = Float.parseFloat(value.replace(",", ".")) * rate; NumberFormat nf = NumberFormat.getIntegerInstance(); nf.setMaximumFractionDigits(2); String _v = nf.format(rc); return _v.replace(".", ","); }//from w ww.ja va2 s .co m return value; }
From source file:com.skubit.iab.activities.TransactionDetailsActivity.java
private static final String formatCurrencyAmount(BigDecimal balanceNumber) { NumberFormat numberFormat = NumberFormat.getInstance(Locale.getDefault()); numberFormat.setMaximumFractionDigits(8); numberFormat.setMinimumFractionDigits(4); if (balanceNumber.compareTo(BigDecimal.ZERO) == -1) { balanceNumber = balanceNumber.multiply(new BigDecimal(-1)); }//from w w w. j ava 2s . co m return numberFormat.format(balanceNumber); }
From source file:com.actinarium.kinetic.pipeline.CodeGenerator.java
/** * Generates Java code for a table lookup interpolator based on provided values * * @param packageName package name to write into the template * @param className class name to write into the template * @param sourceName title of the measurement the values were taken from * @param values an array of float values that must be recorded at equal intervals * @return generated drop-in Java code//w w w. j av a 2 s. co m */ public static String generateInterpolatorCode(String packageName, String className, String sourceName, float[] values) { NumberFormat format = DecimalFormat.getNumberInstance(Locale.ROOT); format.setMinimumFractionDigits(4); format.setMaximumFractionDigits(4); StringBuilder valuesBuilder = new StringBuilder(CHARS_PER_LINE * (values.length / VALUES_PER_ROW + 1)); // Append all values but the last one final int lengthMinusOne = values.length - 1; for (int i = 0; i < lengthMinusOne; /* incremented in loop body */) { if (values[i] > 0) { // Append space before positive numbers to align with those having minus sign valuesBuilder.append(' '); } valuesBuilder.append(format.format(values[i])).append('f').append(','); if (++i % VALUES_PER_ROW == 0) { valuesBuilder.append("\n "); } else { valuesBuilder.append(' '); } } // Append last value valuesBuilder.append(format.format(values[lengthMinusOne])).append('f'); // and generate Java code out of the given template return String.format(TABLE_LOOKUP_TEMPLATE, packageName, className, sourceName, valuesBuilder.toString()); }
From source file:Main.java
private static JSpinner makeDigitsOnlySpinnerUsingDocumentFilter() { JSpinner spinner = new JSpinner(new SpinnerNumberModel()); JSpinner.NumberEditor jsEditor = (JSpinner.NumberEditor) spinner.getEditor(); JFormattedTextField textField = jsEditor.getTextField(); DocumentFilter digitOnlyFilter = new DocumentFilter() { @Override//from w ww .j a v a 2 s . co m public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException { if (stringContainsOnlyDigits(string)) { super.insertString(fb, offset, string, attr); } } @Override public void remove(FilterBypass fb, int offset, int length) throws BadLocationException { super.remove(fb, offset, length); } @Override public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException { if (stringContainsOnlyDigits(text)) { super.replace(fb, offset, length, text, attrs); } } private boolean stringContainsOnlyDigits(String text) { for (int i = 0; i < text.length(); i++) { if (!Character.isDigit(text.charAt(i))) { return false; } } return true; } }; NumberFormat format = NumberFormat.getPercentInstance(); format.setGroupingUsed(false); format.setGroupingUsed(true); format.setMaximumIntegerDigits(10); format.setMaximumFractionDigits(2); format.setMinimumFractionDigits(5); textField.setFormatterFactory(new DefaultFormatterFactory(new InternationalFormatter(format) { @Override protected DocumentFilter getDocumentFilter() { return digitOnlyFilter; } })); return spinner; }
From source file:Main.java
/** * /*w ww .jav a 2 s. c om*/ * @param value * @param min_fraction_digit * @param max_fraction_digit * @return */ public static String formatNumber(double value, int min_fraction_digit, int max_fraction_digit) { NumberFormat nf; nf = NumberFormat.getInstance(); if (min_fraction_digit != 0) { nf.setMinimumFractionDigits(min_fraction_digit); } if (max_fraction_digit != 0) { nf.setMaximumFractionDigits(max_fraction_digit); } return nf.format(value); }
From source file:eu.choreos.vv.chart.XYChart.java
private static JFreeChart createChart(XYDataset dataset, String chartTitle, String xLabel, String yLabel) { // create the chart... JFreeChart chart = ChartFactory.createXYLineChart(chartTitle, // chart title xLabel, // domain axis label yLabel, // range axis label dataset, // initial series PlotOrientation.VERTICAL, // orientation true, // include legend true, // tooltips? false // URLs? );// ww w. j ava 2 s .c o m // set chart background chart.setBackgroundPaint(Color.white); // set a few custom plot features XYPlot plot = (XYPlot) chart.getPlot(); plot.setBackgroundPaint(new Color(0xffffe0)); plot.setDomainGridlinesVisible(true); plot.setDomainGridlinePaint(Color.lightGray); plot.setRangeGridlinePaint(Color.lightGray); // set the plot's axes to display integers TickUnitSource ticks = NumberAxis.createIntegerTickUnits(); NumberAxis domain = (NumberAxis) plot.getDomainAxis(); domain.setStandardTickUnits(ticks); domain.resizeRange(1.1); domain.setLowerBound(0.5); NumberAxis range = (NumberAxis) plot.getRangeAxis(); range.setStandardTickUnits(ticks); range.setUpperBound(range.getUpperBound() * 1.1); // render shapes and lines XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true, true); plot.setRenderer(renderer); renderer.setBaseShapesVisible(true); renderer.setBaseShapesFilled(true); // set the renderer's stroke Stroke stroke = new BasicStroke(3f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL); renderer.setBaseOutlineStroke(stroke); // label the points NumberFormat format = NumberFormat.getNumberInstance(); format.setMaximumFractionDigits(2); XYItemLabelGenerator generator = new StandardXYItemLabelGenerator( StandardXYItemLabelGenerator.DEFAULT_ITEM_LABEL_FORMAT, format, format); renderer.setBaseItemLabelGenerator(generator); renderer.setBaseItemLabelsVisible(true); return chart; }