0

double、integer、ascii、または byte 値を含む文字列値があり、その値を JLabel に入れました。double 値と long 値を4000000000000、Java JLabel のデフォルトの印刷スタイルである ではなく、の形式にしたいと考えています4.0E12。これで、文字列のデータ型はわかりましたが、JLabel に double 値と integer 値の非科学的な形式のみを表示させる方法がわかりません。

これが私がこれまでに試したことです:

String str = value; // string that holds the value

switch (var) // var that says which data type my str is
{
  case LONG:
  //convert my string from scientific to non-scientific here
  break;
  case DOUBLE:
  //convert my string from scientific to non-scientific here
  break;
  case ASCII:
  //do nothing
  break;
  ...
}

JLabel label = new JLabel();
label.setText(str); //Want this to be in non-scientific form

しかし、この方法はまだ科学的な形式を出力するだけです。

編集:

私の変換は次のようになります。

str = new DecimalFormat("#0.###").format(str);

また、はい、それは長い値です。明確にするために、データ型変数の一部を省略しました。これがすべてのケースで機能するかどうかはわかりませんが、機能したとしても. integer、long、xtended、double、および float で機能する必要があります。

4

1 に答える 1

1

デフォルトでは変換を行わないため、別の JLabel を使用している必要があります

JFrame frame = new JFrame();
JLabel label = new JLabel();
DecimalFormat df = new DecimalFormat("#0.###");
label.setText(df.format(4e12));
frame.add(label);
frame.pack();
frame.setVisible(true);

ウィンドウを表示します

4000000000000

その変換で次のようになります

DecimalFormat df = new DecimalFormat("#0.###");
System.out.println(df.format(400000000));
System.out.println(df.format(4000000000000L));
System.out.println(df.format(4e12f));
System.out.println(df.format(4e12));

版画

400000000
4000000000000
3999999983616   <- due to float rounding error.
4000000000000
于 2011-06-08T15:02:52.840 に答える