List of usage examples for java.text NumberFormat setMinimumFractionDigits
public void setMinimumFractionDigits(int newValue)
From source file:Main.java
public static void main(String[] argv) throws Exception { NumberFormat nf = NumberFormat.getInstance(); nf.setMinimumFractionDigits(2); System.out.println("Format with two decimal places: " + nf.format(1234567.678)); }
From source file:Main.java
public static void main(String[] argv) throws Exception { NumberFormat nf = NumberFormat.getInstance(); nf.setMinimumFractionDigits(2); System.out.println("Format with two decimal places: " + nf.format(1234567.678)); System.out.println("Format with two decimal places: " + nf.format(1234567.6)); }
From source file:Main.java
public static void main(String args[]) { JFrame frame = new JFrame(); final JFormattedTextField textField1 = new JFormattedTextField(new Float(10.01)); textField1.setFormatterFactory(new AbstractFormatterFactory() { @Override//from w ww. j av a2 s.c om public AbstractFormatter getFormatter(JFormattedTextField tf) { NumberFormat format = DecimalFormat.getInstance(); format.setMinimumFractionDigits(2); format.setMaximumFractionDigits(2); format.setRoundingMode(RoundingMode.HALF_UP); InternationalFormatter formatter = new InternationalFormatter(format); formatter.setAllowsInvalid(false); formatter.setMinimum(0.0); formatter.setMaximum(1000.00); return formatter; } }); NumberFormat numberFormat = NumberFormat.getNumberInstance(); numberFormat.setMaximumFractionDigits(2); numberFormat.setMaximumFractionDigits(2); numberFormat.setRoundingMode(RoundingMode.HALF_UP); final JFormattedTextField textField2 = new JFormattedTextField(numberFormat); textField2.setValue(new Float(10.01)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(textField1, BorderLayout.NORTH); frame.add(textField2, BorderLayout.SOUTH); frame.setVisible(true); frame.pack(); }
From source file:Main.java
public static void main(String[] args) throws Exception { NumberFormat numberFormat = NumberFormat.getNumberInstance(); numberFormat.setCurrency(Currency.getInstance(Locale.CANADA)); numberFormat.setMinimumFractionDigits(2); }
From source file:NumFormat2.java
/** The main (and only) method in this class. */ public static void main(String[] av) { // Get a format instance NumberFormat form = NumberFormat.getInstance(); // Set it to look like 999.99[99] form.setMinimumIntegerDigits(3);/*w ww.j a va 2 s . c o m*/ form.setMinimumFractionDigits(2); form.setMaximumFractionDigits(4); // Now print using it. for (int i = 0; i < data.length; i++) System.out.println(data[i] + "\tformats as " + form.format(data[i])); }
From source file:MainClass.java
public static void main(String[] av) { double data[] = { 0, 1, 22d / 7, 100.2345678 }; NumberFormat form = NumberFormat.getInstance(); // Set it to look like 999.99[99] form.setMinimumIntegerDigits(3);// w ww .j a v a2s .c om form.setMinimumFractionDigits(2); form.setMaximumFractionDigits(4); // Now print using it. for (int i = 0; i < data.length; i++) System.out.println(data[i] + "\tformats as " + form.format(data[i])); }
From source file:Main.java
public static void main(String[] args) throws Exception { NumberFormat format = NumberFormat.getNumberInstance(); format.setMaximumFractionDigits(2);/* w w w.j av a2 s .co m*/ format.setMinimumFractionDigits(2); format.setParseIntegerOnly(true); format.setRoundingMode(RoundingMode.HALF_UP); NumberFormatter formatter = new NumberFormatter(format); formatter.setMaximum(1000); formatter.setMinimum(0.0); formatter.setAllowsInvalid(false); // formatter.setOverwriteMode(false); JFormattedTextField tf = new JFormattedTextField(formatter); tf.setColumns(10); tf.setValue(123456789.99); JFormattedTextField tf1 = new JFormattedTextField(formatter); tf1.setValue(1234567890.99); JFormattedTextField tf2 = new JFormattedTextField(formatter); tf2.setValue(1111.1111); JFormattedTextField tf3 = new JFormattedTextField(formatter); tf3.setValue(-1111.1111); JFormattedTextField tf4 = new JFormattedTextField(formatter); tf4.setValue(-56); JFrame frame = new JFrame("Test"); frame.setLayout(new GridLayout(5, 0)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(tf); frame.add(tf1); frame.add(tf2); frame.add(tf3); frame.add(tf4); frame.pack(); frame.setVisible(true); }
From source file:Main.java
public static void main(String args[]) { JFrame frame = new JFrame(); final JFormattedTextField textField1 = new JFormattedTextField(new Float(10.01)); textField1.setFormatterFactory(new AbstractFormatterFactory() { @Override/* ww w .j a v a 2 s . c om*/ public AbstractFormatter getFormatter(JFormattedTextField tf) { NumberFormat format = DecimalFormat.getInstance(); format.setMinimumFractionDigits(2); format.setMaximumFractionDigits(2); format.setRoundingMode(RoundingMode.HALF_UP); InternationalFormatter formatter = new InternationalFormatter(format); formatter.setAllowsInvalid(false); formatter.setMinimum(0.0); formatter.setMaximum(1000.00); return formatter; } }); Map attributes = (new Font("Serif", Font.BOLD, 16)).getAttributes(); attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON); final JFormattedTextField textField2 = new JFormattedTextField(new Float(10.01)); textField2.setFormatterFactory(new AbstractFormatterFactory() { @Override public AbstractFormatter getFormatter(JFormattedTextField tf) { NumberFormat format = DecimalFormat.getInstance(); format.setMinimumFractionDigits(2); format.setMaximumFractionDigits(2); format.setRoundingMode(RoundingMode.HALF_UP); InternationalFormatter formatter = new InternationalFormatter(format); formatter.setAllowsInvalid(false); formatter.setMinimum(0.0); formatter.setMaximum(1000.00); return formatter; } }); textField2.getDocument().addDocumentListener(new DocumentListener() { @Override public void changedUpdate(DocumentEvent documentEvent) { printIt(documentEvent); } @Override public void insertUpdate(DocumentEvent documentEvent) { printIt(documentEvent); } @Override public void removeUpdate(DocumentEvent documentEvent) { printIt(documentEvent); } private void printIt(DocumentEvent documentEvent) { DocumentEvent.EventType type = documentEvent.getType(); double t1a1 = (((Number) textField2.getValue()).doubleValue()); if (t1a1 > 100) { textField2.setFont(new Font(attributes)); textField2.setForeground(Color.red); } else { textField2.setFont(new Font("Serif", Font.BOLD, 16)); textField2.setForeground(Color.black); } } }); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(textField1, BorderLayout.NORTH); frame.add(textField2, BorderLayout.SOUTH); frame.setVisible(true); frame.pack(); }
From source file:invoice.GetInvoice.java
/** * @param args the command line arguments *///from w ww. j a va 2 s . c om public static void main(String[] args) { NumberFormat nf = NumberFormat.getInstance(); nf.setGroupingUsed(false); nf.setMaximumFractionDigits(2); nf.setMinimumFractionDigits(2); nf.setRoundingMode(RoundingMode.HALF_UP); try { JSONObject arg_json = new JSONObject(args[0]); } catch (JSONException ex) { Logger.getLogger(GetInvoice.class.getName()).log(Level.SEVERE, null, ex); } HashMap<String, Object> hm = new HashMap<>(); hm.put("duplicate", ""); hm.put("distributor", "//oshan" + "\n" + "//kapuhempala" + "\n\nArea: " + "//galle"); hm.put("customer", "//owner" + "\n" + "//Agro" + "\n" + "//Agro add" + "\n" + "//0771894851"); hm.put("invNo", "GSLTS" + String.format("%04d", Integer.parseInt("//100"))); hm.put("invDate", "2014-01-10"); hm.put("invCode", "300"); double invoiceTotal = 500000; if (5 > 0) {//ShopDiscount double discountprice = (invoiceTotal * 99) / 100;//getShopDiscount() hm.put("invoiceDiscount", nf.format((invoiceTotal) * 99 / 100));//getRetail_discount() } else { hm.put("invoiceDiscount", ""); } hm.put("gross_total", nf.format(invoiceTotal)); hm.put("invoiceTotal", nf.format(((invoiceTotal) * (100 - 99) / 100)));//getRetail_discount() hm.put("salesPersonName", "rep"); hm.put("salesPersonContactNo", "0772189584"); JTable jTable1 = new JTable(); jTable1.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {}, new String[] { "ITEMCODE", "DESCRIPTION", "QTY", "FREEQTY", "PRICE", "AMOUNT" })); String reportSource = "./ireports/invoice.jrxml"; DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel(); try { JasperReport jr = JasperCompileManager.compileReport(reportSource); JasperPrint jp = JasperFillManager.fillReport(jr, hm, new JRTableModelDataSource(dtm)); JasperPrintManager.printReport(jp, false); } catch (JRException ex) { Logger.getLogger(GetInvoice.class.getName()).log(Level.SEVERE, null, ex); } System.out.println("1"); }
From source file:br.com.thiagomoreira.bancodobrasil.Main.java
/** * @param args/*ww w.j ava 2 s. c o m*/ */ public static void main(String[] args) throws Exception { if (args != null) { NumberFormat formatter = NumberFormat.getNumberInstance(new Locale("pt", "BR")); formatter.setMaximumFractionDigits(2); formatter.setMinimumFractionDigits(2); double total = 0; for (String arg : args) { File input = new File(arg); if (input.exists()) { List<String> lines = IOUtils.readLines(new FileInputStream(input), "ISO-8859-1"); Parser parser = new DefaultParser(); List<Transaction> transactions = parser.parse(lines); TransactionList transactionList = new TransactionList(); transactionList.setStart(parser.getStartDate()); transactionList.setEnd(parser.getEndDate()); transactionList.setTransactions(transactions); CreditCardAccountDetails creditCardAccountDetails = new CreditCardAccountDetails(); creditCardAccountDetails.setAccountNumber("7616-3"); creditCardAccountDetails.setAccountKey(parser.getAccountKey()); CreditCardStatementResponse creditCardStatementResponse = new CreditCardStatementResponse(); creditCardStatementResponse.setAccount(creditCardAccountDetails); creditCardStatementResponse.setCurrencyCode("BRL"); creditCardStatementResponse.setTransactionList(transactionList); Status status = new Status(); status.setCode(Status.KnownCode.SUCCESS); status.setSeverity(Status.Severity.INFO); CreditCardStatementResponseTransaction statementResponse = new CreditCardStatementResponseTransaction(); statementResponse.setClientCookie(UUID.randomUUID().toString()); statementResponse.setStatus(status); statementResponse.setUID(UUID.randomUUID().toString()); statementResponse.setMessage(creditCardStatementResponse); CreditCardResponseMessageSet creditCardResponseMessageSet = new CreditCardResponseMessageSet(); creditCardResponseMessageSet.setStatementResponse(statementResponse); SortedSet<ResponseMessageSet> messageSets = new TreeSet<ResponseMessageSet>(); messageSets.add(creditCardResponseMessageSet); ResponseEnvelope envelope = new ResponseEnvelope(); envelope.setUID(UUID.randomUUID().toString()); envelope.setSecurity(ApplicationSecurity.NONE); envelope.setMessageSets(messageSets); double brazilianRealsamount = parser.getBrazilianRealsAmount(); double dolarsAmount = parser.getDolarsAmount(); double cardTotal = dolarsAmount * parser.getExchangeRate() + brazilianRealsamount; total += cardTotal; System.out.println(creditCardAccountDetails.getAccountKey()); System.out.println("TOTAL EM RS " + formatter.format(brazilianRealsamount)); System.out.println("TOTAL EM US " + formatter.format(dolarsAmount)); System.out.println("TOTAL FATURA EM RS " + formatter.format(cardTotal)); System.out.println(); if (!transactions.isEmpty()) { String parent = System.getProperty("user.home") + "/Downloads"; String fileName = arg.replace(".txt", ".ofx"); File output = new File(parent, fileName); FileOutputStream fos = new FileOutputStream(output); OFXV1Writer writer = new OFXV1Writer(fos); writer.setWriteAttributesOnNewLine(true); AggregateMarshaller marshaller = new AggregateMarshaller(); marshaller.setConversion(new MyFinanceStringConversion()); marshaller.marshal(envelope, writer); writer.flush(); writer.close(); } } } System.out.println("TOTAL FATURAS EM RS " + formatter.format(total)); } }