【问题标题】:TextView not displaying large values in scientific notationTextView 不以科学计数法显示大值
【发布时间】:2013-05-27 07:31:37
【问题描述】:

这是文本视图:

<TextView
android:id="@+id/input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="right"
android:textAlignment="viewEnd"
android:textColor="#ffffff"
android:textSize="50sp"/>

我正在使用:

String inputStr = doubleToString(result);
input.setText(inputStr);

TextView 只是扩展更多行以适应大数字(现在是字符串)。我想那是因为我使用了WRAP_CONTENT 作为高度,但我不能使用FILL_PARENT

如果我使用android:lines="1",那么我根本看不到所有数字。 那么在TextView中显示之前如何将双精度数转换为科学计数法?

编辑

我刚刚发现 它以科学记数法显示,但仅适用于大于 10^12 的值但它们不仅仅是可见的。

所以我只是显示大于 1E10 的值。

【问题讨论】:

标签: java android xml double scientific-notation


【解决方案1】:

像这样将 double 转换为科学记数法

public static String parseToCientificNotation(double result) {
    int cont = 0;
    java.text.DecimalFormat DECIMAL_FORMATER = new java.text.DecimalFormat("0.##");
    while (((int) result) != 0) {
        result/= 10;
        cont++;
    }
    return DECIMAL_FORMATER.format(result).replace(",", ".") + " x10^ -" + cont;

}

【讨论】:

  • 太棒了。它甚至可以格式化较小的数字。我修改了它。谢谢。
猜你喜欢
  • 2011-10-18
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多