简体   繁体   English

如何在SWT表列中设置科学值(EX 123E45、143E-1等)?

[英]How to set scientific value(EX 123E45, 143E-1,…) in the SWT table column?

How to take scientific value in the SWT table column? 如何在SWT表列中获取科学价值? Which data type supported in the java for scientific Notation? Java for Scientific Notation支持哪种数据类型? How to do this? 这个怎么做? Here are simple code for SWT table which i want apply for the scientific notation. 这是我想申请科学计数法的SWT表的简单代码。

package test;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class SortTable {
 private TableRow rows[] = new TableRow[] {
  new TableRow(1, "aaa", new Date(1363784269000 L)),
   new TableRow(2, "abc", new Date(1367784269000 L)),
   new TableRow(3, "efc", new Date(1363584269000 L)),
   new TableRow(4, "ccc", new Date(1363734269000 L)),
 };
 private Table table;
 private TableColumn intColumn;
 private TableColumn strColumn;
 private TableColumn dateColumn;
 private TableColumn scientificColumn;

 public SortTable() {
  Display display = new Display();
  Shell shell = new Shell(display);
  shell.setLayout(new FillLayout());

  table = new Table(shell, SWT.BORDER);
  table.setHeaderVisible(true);
  intColumn = new TableColumn(table, SWT.NONE);
  intColumn.setText("int");
  intColumn.setWidth(50);
  strColumn = new TableColumn(table, SWT.NONE);
  strColumn.setText("string");
  strColumn.setWidth(50);
  dateColumn = new TableColumn(table, SWT.NONE);
  dateColumn.setText("date");
  dateColumn.setWidth(100);

  scientificColumn = new TableColumn(table, SWT.NONE);
  scientificColumn.setText("ScintificValue");

  updateTable();

  Listener sortListener = new Listener() {
   public void handleEvent(Event e) {
    TableColumn column = (TableColumn) e.widget;
    if (column == intColumn) Arrays.sort(rows, BY_VAL);
    if (column == strColumn) Arrays.sort(rows, BY_STR);
    if (column == dateColumn) Arrays.sort(rows, BY_DATE);

    table.setSortColumn(column);
    updateTable();
   }
  };
  intColumn.addListener(SWT.Selection, sortListener);
  strColumn.addListener(SWT.Selection, sortListener);
  dateColumn.addListener(SWT.Selection, sortListener);
  shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300);
  shell.open();
  while (!shell.isDisposed()) {
   if (!display.readAndDispatch())
    display.sleep();
  }
  display.dispose();
 }

 private void updateTable() {
  table.removeAll();
  for (TableRow row: rows) {
   TableItem item = new TableItem(table, SWT.NONE);
   item.setText(row.asString());
  }
 }

 public final Comparator < TableRow > BY_VAL = new Comparator < TableRow > () {
  @Override
  public int compare(TableRow o1, TableRow o2) {
   if (o1.val < o2.val) return -1;
   if (o1.val > o2.val) return 1;
   return 0;
  }
 };

 public final Comparator < TableRow > BY_STR = new Comparator < TableRow > () {
  @Override
  public int compare(TableRow o1, TableRow o2) {
   return o1.str.compareTo(o2.str);
  }
 };

 public final Comparator < TableRow > BY_DATE = new Comparator < TableRow > () {
  @Override
  public int compare(TableRow o1, TableRow o2) {
   return o1.date.compareTo(o2.date);
  }
 };


 private class TableRow {
  private int val;
  private String str;
  private Date date;
  private SimpleDateFormat format = new SimpleDateFormat();


  public TableRow(int val, String str, Date date) {
   this.val = val;
   this.str = str;
   this.date = date;
  }

  public String[] asString() {
   return new String[] {
    Integer.toString(val), str, format.format(date)
   };
  }
 }

 public static void main(String[] args) {
  new SortTable();
 }
}

This is nothing to do with the SWT Table which just shows the string you give it. 这与SWT表无关,后者仅显示您提供的字符串。

It is just how you convert your value to a string in the asString method. 这就是在asString方法asString值转换为字符串的方式。

For values such as 123e45 an integer is not suitable as it cannot hold such a large value you will need to use double . 对于诸如123e45的值, integer不适合,因为它不能容纳这么大的值,您将需要使用double

If you have 如果你有

double val;

you can format it in scientific notation using something like: 您可以使用以下格式将其格式化为科学计数法:

String.format("%.3E", val)

Where "%.3E" specifies scientific notation with 3 decimal places. 其中“%.3E”指定科学计数法,小数点后三位。

See this question for more details on the format string. 有关格式字符串的更多详细信息,请参见此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM