1

My code looks simple and I don't know what is the problem. If I code something like this:

tvResult.setText(sum+ "RUB");

it shows correct numbers. But if I try to add an IF statement like this:

if(sum>=114000) {
                    tvResult.setText(sum + " RUB");
                }

and sum equals, say, 1000000, it shows weird number: 1111111.0. Need your advice =) Thanks in advance

Here is the XML code:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:id="@+id/tvCash"
        android:layout_below="@id/tvCalculate"
        android:hint="@string/cash_money"
        android:background="@drawable/zakat_red"
        android:layout_marginTop="20dp"/>

    <EditText
        android:layout_width="130dp"
        android:layout_height="40dp"
        android:id="@+id/etCash"
       android:inputType="numberDecimal"
        android:layout_above="@+id/tvBank"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:hint="@string/hint_zakat"
        android:gravity="end"
        android:imeOptions="actionDone"/>

And Java code:

TextWatcher twCash=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                tvCash.setBackgroundResource(R.drawable.zakat_green);
                sum+=Float.valueOf(etCash.getText().toString());
                if(sum>=114000) {
                    tvResult.setText(sum + " RUB");
                }
            }
        };
lememele
  • 13
  • 3
  • You mean to say that if you remove if(sum>=114000) , then you don't see decimals in 'tvResult' ? – Shadab Ansari Mar 09 '16 at 21:07
  • no. If I remove if(sum>=114000) then it shows correct numbers. But I need that statement for future use. With that statement it shows incorrect numbers. If the sum==1000000, it says that sum==1111111 – lememele Mar 09 '16 at 21:15

2 Answers2

0

The ".0" is most likely coming up because the sum variable is a float or something. You should either use an int for the variable sum, or you should cast it to an int when you are displaying it. This should work.

if (sum >= 114000) {
    tvResult.setText((int)sum + " RUB");
}

If you need it to be a sum and only show the values after the decimal if they aren't zero, you'll need to write a method to parse the value to return what you want.

Chris Wilson
  • 248
  • 1
  • 9
0

There is problem with your logic. As you are using TextWatcher, to compute the sum of all the numbers entered in your editText, even if you type '.' after any number , afterTextChanged() will get invoked and then it will add that number to the aggregated sum.So let's say , you input the following characters consecutively : 2 , 3, . , 4 , 7 Then you will get the sum = 94.87 And I guess this is not the correct way to compute the sum of the numbers entered. You have to correct your logic to get the desired output.

Shadab Ansari
  • 7,022
  • 2
  • 27
  • 45
  • Sorry, didn't understand =) The variable calls sum, but in this example it doesn't make any additions(It will be in advance. I simplified the example because the problem isn't in calculations, it is in IF statement). I just want to display the input number on tvResult if it is more than 114000. When I input 1000000 it shows 1111111 if I use an IF statement and it shows correct numbers if I don't use it. The IF statement changes something... – lememele Mar 09 '16 at 22:03
  • Please add logs immediately before the if condition and inside the if condition - Log.d("debug","SUM::" + sum); and check whether they depict the same values or different. – Shadab Ansari Mar 09 '16 at 22:22
  • Yes, it is different. It adds one additional 1 to each input number =) I understood what you said about aggregations. Seems like the problem isn't in the IF statement and I misunderstood how to use TextWatcher =) Thanks for your help! – lememele Mar 09 '16 at 22:28
  • I upvoted, but it will be displayed later when I get more reputation =) – lememele Mar 09 '16 at 22:38