当前位置: 技术问答>java相关
java的JTABLE列怎样设小数保留位和四舍五入。例如设为保留两位小数,回答了马上给分
来源: 互联网 发布时间:2015-02-20
本文导语: java的JTABLE列怎样设小数保留位和四舍五入。例如设为保留两位小数,回答了马上给分 | 保留两位小数可以采用如下算法: double dbl = 3.3123143 Math.round(dbl*100)/100.00 | 有几种方...
java的JTABLE列怎样设小数保留位和四舍五入。例如设为保留两位小数,回答了马上给分
|
保留两位小数可以采用如下算法:
double dbl = 3.3123143
Math.round(dbl*100)/100.00
double dbl = 3.3123143
Math.round(dbl*100)/100.00
|
有几种方法都可实现
1。
import java.text.DecimalFormat;
String a = new DecimalFormat("###,###,###.##").format(100.515 );
//System.out.println("the a is:"+a); a=100.52
2.
import java.text.DecimalFormat;
private static DecimalFormat format = new DecimalFormat();
format.applyPattern("###,###.##");
String a = format.format(100.515);
//System.out.println("the a is:"+a); a=100.52
以上是简单的例子,我没有写全,不过你可以看出用法,也可根据需要,自己写个方法。
要是好用了,别忘给分。
1。
import java.text.DecimalFormat;
String a = new DecimalFormat("###,###,###.##").format(100.515 );
//System.out.println("the a is:"+a); a=100.52
2.
import java.text.DecimalFormat;
private static DecimalFormat format = new DecimalFormat();
format.applyPattern("###,###.##");
String a = format.format(100.515);
//System.out.println("the a is:"+a); a=100.52
以上是简单的例子,我没有写全,不过你可以看出用法,也可根据需要,自己写个方法。
要是好用了,别忘给分。
|
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
/**
* @version 1.0 12/03/98
*/
class DecimalRenderer extends DefaultTableCellRenderer {
DecimalFormat formatter;
DecimalRenderer(String pattern) {
this(new DecimalFormat(pattern));
}
DecimalRenderer(DecimalFormat formatter) {
this.formatter = formatter;
setHorizontalAlignment(JLabel.RIGHT);
}
public void setValue(Object value) {
setText((value == null) ? ""
: formatter.format(((Double)value).doubleValue()));
}
}
public class TotalRowExample extends JFrame {
final private int TOTAL_ROW = 3;
final private int TOTAL_COLUMN = 1;
TotalRowExample() {
super( "Total Row Example" );
final DecimalFormat formatter = new DecimalFormat("###,##0.00");
DefaultTableModel dm = new DefaultTableModel() {
public void setValueAt(Object value, int row, int col) {
Vector rowVector = (Vector)dataVector.elementAt(row);
if (col == TOTAL_COLUMN) {
Double d = null;
if (value instanceof Double) {
d = (Double)value;
} else {
try {
d = new Double(
((Number)formatter.parse((String)value)).doubleValue());
} catch (ParseException ex) {
d = new Double(0.0);
}
}
rowVector.setElementAt(d, col);
} else {
rowVector.setElementAt(value, col);
}
}
public boolean isCellEditable(int row, int col) {
if (row == TOTAL_ROW) return false;
return true;
}
public Class getColumnClass(int col) {
if (col == TOTAL_COLUMN) return Number.class;
return String.class;
}
};
dm.setDataVector(
new Object[][]{
{"coffee",new Double(0.0)},
{"tea" ,new Double(0.0)},
{"cocoa" ,new Double(0.0)},
{"total" ,new Double(0.0)}},
new Object[]{"Item","Price"});
JTable table = new JTable( dm ) {
public void editingStopped(ChangeEvent e) {
super.editingStopped(e);
reCalcurate(getModel());
repaint();
}
};
table.getColumn("Price").setCellRenderer(
new DecimalRenderer(formatter));
JScrollPane scroll = new JScrollPane(table);
Container content = getContentPane();
content.add(scroll);
setSize( 300, 120 );
setVisible(true);
}
private void reCalcurate(TableModel ml) {
if (ml == null) return;
double total = 0.0;
for (int i=0;i
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
/**
* @version 1.0 12/03/98
*/
class DecimalRenderer extends DefaultTableCellRenderer {
DecimalFormat formatter;
DecimalRenderer(String pattern) {
this(new DecimalFormat(pattern));
}
DecimalRenderer(DecimalFormat formatter) {
this.formatter = formatter;
setHorizontalAlignment(JLabel.RIGHT);
}
public void setValue(Object value) {
setText((value == null) ? ""
: formatter.format(((Double)value).doubleValue()));
}
}
public class TotalRowExample extends JFrame {
final private int TOTAL_ROW = 3;
final private int TOTAL_COLUMN = 1;
TotalRowExample() {
super( "Total Row Example" );
final DecimalFormat formatter = new DecimalFormat("###,##0.00");
DefaultTableModel dm = new DefaultTableModel() {
public void setValueAt(Object value, int row, int col) {
Vector rowVector = (Vector)dataVector.elementAt(row);
if (col == TOTAL_COLUMN) {
Double d = null;
if (value instanceof Double) {
d = (Double)value;
} else {
try {
d = new Double(
((Number)formatter.parse((String)value)).doubleValue());
} catch (ParseException ex) {
d = new Double(0.0);
}
}
rowVector.setElementAt(d, col);
} else {
rowVector.setElementAt(value, col);
}
}
public boolean isCellEditable(int row, int col) {
if (row == TOTAL_ROW) return false;
return true;
}
public Class getColumnClass(int col) {
if (col == TOTAL_COLUMN) return Number.class;
return String.class;
}
};
dm.setDataVector(
new Object[][]{
{"coffee",new Double(0.0)},
{"tea" ,new Double(0.0)},
{"cocoa" ,new Double(0.0)},
{"total" ,new Double(0.0)}},
new Object[]{"Item","Price"});
JTable table = new JTable( dm ) {
public void editingStopped(ChangeEvent e) {
super.editingStopped(e);
reCalcurate(getModel());
repaint();
}
};
table.getColumn("Price").setCellRenderer(
new DecimalRenderer(formatter));
JScrollPane scroll = new JScrollPane(table);
Container content = getContentPane();
content.add(scroll);
setSize( 300, 120 );
setVisible(true);
}
private void reCalcurate(TableModel ml) {
if (ml == null) return;
double total = 0.0;
for (int i=0;i