Robert Robert - 3 months ago 10
Java Question

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

I'm not even sure how to begin, but I'll do my best to explain myself.

in XML I have to EditTexts :

<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"
/>


and

<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"
/>


The first defined as number, the second one as numberDecimal. In the first one I only allow round numbers to be inserted, in the second one I also need decimals.
Now, in 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 ) );
}
});
}


Here (it is more obvious for you than for a newbie like me) are another 2 variables defined, TextEdit values taken and the math done. (thanks to another stackoverflow user).

Now, on the below lines I output the values to the user :

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");}


Now for the question part of the story :)


  1. Both results return a value like : 36.641221374045806 (and the second one : 3.2749999999999995) . The number od decimals shown is astronomical. 4 or 5 decimals are more than enough for me and for the other part of the math that I'm unable to do. So, the question is : How can I limit the number of decimals shown on the screen to the user, using the code that I allready have.

  2. From the value resulted doing the math for "displayTime", the value before the comma I want to display it on another field as it is but, the decimals resulted after the first math operation I have to multiply it with 0.6 ant the result added as decimals for the value before the comma.



I hope that at least one of you got the picture as I see it and as I tried my best to explain it.
I know I'm asking much but after another few hours of searching I came out empty handed. Tried all I could find and think of but since the BIG limitations in my first week of Java and XML I can go any further with my app.

Thank you all for the time, at least for reading this story and then, maybe making the time answering and explaining ...

Best regards,
Robert

Answer

I didn't understand your second question properly, but for the question one you can use a DecimalFormat as below:

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

You can always manipulate the string to give you the results you require.

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

I was under the impression that you had to display it in a TextField. Sorry. You can always parse this String to Double value.

Double value = Double.parseDouble(your_string);

With all these type conversions done, I do think there might have been a better way to implement the same.

Comments