限制显示的小数位数,仅取小数以继续数学运算

Limiting number of decimals shown and take only the decimals to continue a math operation

我什至不确定如何开始,但我会尽力解释自己。

在 XML 我必须编辑文本 :

<EditText
        android:id="@+id/auftraggesamt_edit_text"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:background="#616161"
        android:imeOptions="actionDone"
        android:inputType="number"
        android:maxLength="5"
        android:textAlignment="textEnd"
        android:textColor="#ffffff"
        android:textCursorDrawable="@color/colorWhite"
        android:textStyle="bold"
        />

<EditText
        android:id="@+id/vorgabezeit"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:background="#616161"
        android:imeOptions="actionDone"
        android:inputType="numberDecimal"
        android:maxLength="5"
        android:textAlignment="textEnd"
        android:textColor="#ffffff"
        android:textCursorDrawable="@color/colorWhite"
        android:textStyle="bold"
        />

第一个定义为数字,第二个定义为numberDecimal。在第一个中我只允许插入整数,在第二个中我还需要小数。 现在,在 Java 中:

public class MainActivity extends AppCompatActivity {

int procente = 120;
int mitarbeiter = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    findViewById(R.id.button)
            .setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    displayTarget((procente / Double.parseDouble(((EditText) findViewById(R.id.vorgabezeit))
                            .getText().toString())) * mitarbeiter) ;
                    displayTime((Double.parseDouble(((EditText) findViewById(R.id.auftraggesamt_edit_text))
                            .getText().toString())) / ( procente /  Double.parseDouble(((EditText) findViewById(R.id.vorgabezeit))
                            .getText().toString()) * mitarbeiter ) );
                }
            });
}

这里(这对你来说比对像我这样的新手来说更明显)定义了另外 2 个变量,采用了 TextEdit 值并完成了数学运算。 (感谢另一位 Whosebug 用户)。

现在,在下面几行中,我将值输出给用户:

private void displayTarget(double number) {
    TextView priceTextView = (TextView) findViewById(R.id.rezultat);
    priceTextView.setText(number + "  stück/std.");}

private void displayTime(double timp) {
    TextView timeTextView = (TextView) findViewById(R.id.rezultat_ore);
    timeTextView.setText(timp + "  std");}

现在是故事的问题部分:)

  1. 两个结果 return 的值如下:36.641221374045806(第二个:3.2749999999999995)。显示的小数位数是天文数字。 4 或 5 位小数对我和我做不到的其他数学部分来说绰绰有余。所以,问题是:如何使用我已经拥有的代码限制屏幕上显示给用户的小数位数。

  2. 根据对 "displayTime" 进行数学运算的结果,逗号前的值我想按原样显示在另一个字段上,但是,小数点是在第一次数学运算之后产生的我必须将它乘以 0.6 ant 结果作为逗号前的值的小数添加。

我希望你们中至少有一个人得到了我所看到的图片,并且我已尽力解释它。 我知道我问的很多,但又搜索了几个小时后,我空手而归。尝试了所有我能找到和想到的东西,但由于我在 Java 和 XML 的第一周有很大的限制,我可以继续使用我的应用程序。

感谢大家花时间阅读这个故事,也许还有时间回答和解释...

此致, 罗伯特

回答你问题的第一部分,下面的代码会根据你选择的精度限制小数点后显示的位数。 注意这个 returns 字符串并相应地四舍五入。

public static String decimalFormatter(Double number, int precision) {
        String pattern = "0.0";
        for (int i = 1; i < precision; i++) {
            pattern = pattern + "0";
        }
        DecimalFormat df = new DecimalFormat(pattern);
        df.setRoundingMode(RoundingMode.DOWN);
        return df.format(number);
}

我没有正确理解你的第二个问题,但是对于第一个问题你可以使用如下的 DecimalFormat:

DecimalFormat format = new DecimalFormat();
format.setMaximumFractionDigits(2);
String formattedText = format.format(your_variable);

您可以随时操纵字符串以获得所需的结果。

    String full = "45.98";
    String substr1=full.substring(full.indexOf(".") + 1); //will return 98
    String substr2=full.substring(0, full.indexOf(".")); //willreturn 45

我的印象是您必须在 TextField 中显示它。对不起。 您始终可以将此 String 解析为 Double 值。

Double value = Double.parseDouble(your_string);

完成所有这些类型转换后,我确实认为可能有更好的方法来实现相同的转换。