This GitLab instance reached the end of its service life. It won't be possible to create new users or projects.

Please read the deprecation notice for more information concerning the deprecation timeline

Visit migration.git.tu-berlin.de (internal network only) to import your old projects to the new GitLab platform 📥

Commit 19f393a3 by Nico Suhl

parse decimal values correctly, fixes #1 and #4

parent d117f3d2
package de.k4ever.k4android.fragments;
import java.io.IOException;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
......@@ -41,6 +43,8 @@ public class ManageBalanceFragment extends Fragment {
View view = inflater.inflate(R.layout.layout_manage_balance, container, false);
mSum = (EditText)view.findViewById(R.id.sum);
mSum.setText(String.format("%.2f", 1f));
balanceSpinner = (Spinner)view.findViewById(R.id.chargetype);
Button incButton = (Button)view.findViewById(R.id.inc);
Button decButton = (Button)view.findViewById(R.id.dec);
......@@ -48,18 +52,20 @@ public class ManageBalanceFragment extends Fragment {
incButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
mSum.setText(String.format("%.2f", ( Float.parseFloat(mSum.getText().toString()) + 1f )));
} catch (NumberFormatException e) {}
float currentSum = NumberFormat.getInstance().parse(mSum.getText().toString()).floatValue();
mSum.setText(String.format("%.2f", currentSum + 1f));
} catch (ParseException e) {}
}
});
decButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
float currentSum = (Float.parseFloat(mSum.getText().toString()));
if (currentSum > 1)
mSum.setText(String.format("%.2f", currentSum -1f));
} catch (NumberFormatException e) {}
float currentSum = NumberFormat.getInstance().parse(mSum.getText().toString()).floatValue();
if (currentSum > 1) {
mSum.setText(String.format("%.2f", currentSum - 1f));
}
} catch (ParseException e) {}
}
});
......@@ -79,16 +85,24 @@ public class ManageBalanceFragment extends Fragment {
public void submit() {
boolean validForm = false;
NumberFormat numFormat = NumberFormat.getInstance();
float amount = 0;
try {
if(Float.parseFloat(mSum.getText().toString()) > 0f)
validForm = true;
} catch (NumberFormatException e) {e.printStackTrace();}
amount = numFormat.parse(mSum.getText().toString()).floatValue();
if (amount > 0f) {
validForm = true;
}
} catch (ParseException e) {
e.printStackTrace();
}
if(!validForm)
Toast.makeText(getActivity(), R.string.invalid_sum, Toast.LENGTH_SHORT).show();
if (!validForm) {
Toast.makeText(getActivity(), R.string.invalid_sum, Toast.LENGTH_SHORT).show();
}
else {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
float amount = Float.parseFloat(mSum.getText().toString());
// amount * -1 to withdraw
if (balanceSpinner.getSelectedItemId() == 1)
amount *= -1;
......
package de.k4ever.k4android.fragments;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
......@@ -37,6 +39,8 @@ public class VirtualsFragment extends Fragment {
View view = inflater.inflate(R.layout.layout_virtuals, container, false);
mSum = (EditText)view.findViewById(R.id.sum);
mSum.setText(String.format("%.2f", 1f));
mRecipient = (EditText)view.findViewById(R.id.receiver);
mComment = (EditText)view.findViewById(R.id.comment);
Button incButton = (Button)view.findViewById(R.id.inc);
......@@ -56,16 +60,20 @@ public class VirtualsFragment extends Fragment {
incButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
mSum.setText(String.format("%.2f", ( Float.parseFloat(mSum.getText().toString()) + 1f )));
} catch (NumberFormatException e) {}
float currentSum = NumberFormat.getInstance().parse(mSum.getText().toString()).floatValue();
mSum.setText(String.format("%.2f", currentSum + 1f));
} catch (ParseException e) {}
}
});
decButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
mSum.setText(String.format("%.2f", ( Float.parseFloat(mSum.getText().toString()) - 1f )));
} catch (NumberFormatException e) {}
float currentSum = NumberFormat.getInstance().parse(mSum.getText().toString()).floatValue();
if (currentSum > 1) {
mSum.setText(String.format("%.2f", currentSum - 1f));
}
} catch (ParseException e) {}
}
});
......@@ -94,18 +102,27 @@ public class VirtualsFragment extends Fragment {
public void submit() {
boolean validForm = false;
NumberFormat numFormat = NumberFormat.getInstance();
float amount = 0;
try {
if(Float.parseFloat(mSum.getText().toString()) > 0f)
validForm = true;
} catch (NumberFormatException e) {e.printStackTrace();}
if(!validForm)
Toast.makeText(getActivity(), R.string.invalid_sum, Toast.LENGTH_SHORT).show();
else if(mRecipient.getText().length() <= 0)
Toast.makeText(getActivity(), R.string.invalid_recipient, Toast.LENGTH_SHORT).show();
amount = numFormat.parse(mSum.getText().toString()).floatValue();
if (amount > 0f) {
validForm = true;
}
} catch (ParseException e) {
e.printStackTrace();
}
if(!validForm) {
Toast.makeText(getActivity(), R.string.invalid_sum, Toast.LENGTH_SHORT).show();
}
else if(mRecipient.getText().length() <= 0) {
Toast.makeText(getActivity(), R.string.invalid_recipient, Toast.LENGTH_SHORT).show();
}
else {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("amount", mSum.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("amount", Float.toString(amount)));
nameValuePairs.add(new BasicNameValuePair("recipient", mRecipient.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("comment", mComment.getText().toString()));
......
......@@ -17,7 +17,6 @@
android:layout_toLeftOf="@+id/inc"
android:layout_alignParentLeft="true"
android:inputType="numberDecimal"
android:text = "1.00"
android:hint="@string/buyscreen_deposit" />
<Button
android:id="@+id/inc"
......
......@@ -54,8 +54,7 @@
android:layout_toLeftOf="@+id/inc"
android:layout_toRightOf="@+id/money"
android:imeOptions="actionSend"
android:inputType="numberDecimal"
android:text="@string/default_sum" />
android:inputType="numberDecimal" />
<Button
android:id="@+id/inc"
android:layout_width="wrap_content"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment