0

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

Robert
  • 103
  • 1
  • 9
  • Try to be more concise and limit question and code to your actual problem of number formatting. I would look for parameters in the function doc that will allow to specify precision. If that's not available, modify the number string, e.g. search for decimal point position and then remove excess characters. – handle Aug 19 '16 at 08:36
  • you should take a look [here](http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java), **but** IMHO I guess results you see are wrong because you're experiencing [floating point precision error](http://stackoverflow.com/questions/322749/retain-precision-with-double-in-java) – Jordi Castilla Aug 19 '16 at 08:39

2 Answers2

1

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.

SWAT
  • 957
  • 7
  • 17
  • Thank you, that did it ! :D – Robert Aug 19 '16 at 09:01
  • Regarding the second question : I have to split the resulted number : ex : 34.98 into 34 and 98 . 34 I will use it as it is in it's present form, the 98 part i will multiply with 0.6 ant the result I will ad as decimals to 34. and the final result will be 34.(98*0.6) = 34.588 ... or 34.59 – Robert Aug 19 '16 at 09:04
  • Glad I could help. – SWAT Aug 20 '16 at 12:42
  • One more thing, if it is possible, regarding the second part of the question. I just observed a bug : I get a result of 5.3 (instead of 5.30) that later I have to transform : 5 (30*0.6) = 5 18 but because I get 5.3 the end result is : 5 (3*0.6) = 5 1.8 . somehow the 0 in end is cut off, and that is bad for the last part of the math ... – Robert Aug 20 '16 at 14:14
  • using this : format.setMinimumFractionDigits(2); I succeded to show on the rezult 5.30 but for the second part, where I need to take the 30 and multiply it with 0.6, it still remains 3 instead of 30 :( – Robert Aug 20 '16 at 15:39
  • using this : format.setMinimumFractionDigits(2); I succeded to show on the rezult 5.30 but for the second part, where I need to take the 30 and multiply it with 0.6, it still remains 3 instead of 30 :(. another thing : I get the value 30 as string, but to be able to miltiply it with .6 i transform it to double : double minutereal = Double.parseDouble(substr1); TextView timeTextView = (TextView) findViewById(R.id.rezultat_ore_real); timeTextView.setText(substr2 + " stunde, " + minutereal * 0.6 + " minute"); I have tried to transform in integer insted of double. same thing – Robert Aug 20 '16 at 15:48
0

To answer the first part of your question, the below code will limit the number of digits displayed after the decimal point according to the precision of your choice. Note this returns a string and rounds it accordingly.

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);
}
Abhishek
  • 2,345
  • 2
  • 16
  • 24