I have some code calculating two numbers as below, the result gives the string as I need it, but I want to include the pound symbol before the resulting number as it is a financial calculation (hours worked x hourly rate). How do I get the £ symbol to appear before the result? (want the result to say £1000 not 1000)
Any help much appreciated guys :-)
My current code below,
public void buttoncalcrates(View view) {
int d1, d2, thesum;
EditText e1 = (EditText) findViewById(R.id.editText4);
EditText e2 = (EditText) findViewById(R.id.hourlyrate);
TextView t1 = (TextView) findViewById(R.id.textView5);
d1 = Integer.parseInt(e1.getText().toString());
d2=Integer.parseInt(e2.getText().toString());
thesum= d1 * d2;
t1.setText(Integer.toString(thesum));
String.format(...) is intended for this purpose:
t1.setText(String.format("£%d", thesum));
More info about different formatting options can be found in the docs.
Related
This is the interface of an application i am trying to succeed the last days.
It is just a simple counter, of a card game. I have managed to create the buttons, do the counting and show it in a different textview, stoping in a limit i wanted.
The thing is, that i want the scores of each round separetely, to be shown until the end of the game, like a list i draw in ms paint for preview.
I have managed to put the score in the 1st round on the txt1 and txt2 fields, but i cant figure out, how to put the 2nd round on txt3 and txt4 field, the 3rd on txt5 and txt6 field etc.
This is the code i ve created, of the void that puts the txt1 and txt2 into that fields.
private void setScore() {
Double skor1 = Double.parseDouble(editText1.getText().toString());
Double skor2 = Double.parseDouble(editText2.getText().toString());
txt1.setText(Double.toString(skor1));
txt2.setText(Double.toString(skor2));
while (telikoOmada1 < 64 && telikoOmada2 < 64) {
skor1 = Double.parseDouble(editText1.getText().toString());
skor2 = Double.parseDouble(editText2.getText().toString());
telikoOmada1 = telikoOmada1 + skor1;
telikoOmada2 = telikoOmada2 + skor2;
telikoTxt1.setText(Double.toString(telikoOmada1));
telikoTxt2.setText(Double.toString(telikoOmada2));
editText1.setText(null);
editText2.setText(null);
break;
}
}
I am using Double data type for a variable in my Android app.
It simply takes a number and shows whatever percentage increase from that number would be:
for example 1,000,000 plus 1000 % = 1.1E7
The problem is I don't want an exponent display (the E), I want it to be in decimal.
This is a code snippet of the area which when the user clicks a Calculate button the info is displayed in an editText (Textbox)
enter code here
Button calc2 = (Button)findViewById(R.id.button1);
calc2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
EditText number1 = (EditText)findViewById(R.id.editText1);
EditText number2 = (EditText)findViewById(R.id.editText2);
EditText number3 = (EditText)findViewById(R.id.editText3);
double editText1 = Double.parseDouble(number1.getText().toString());
double Pluspercent = Double.parseDouble(number2.getText().toString());
double editText3 = Double.parseDouble(number3.getText().toString());
double Result1 = 0 ;
double Result2 = 0;
Result1 = Pluspercent * 0.01 ;
Result2 = editText1 * Result1;
editText3 = editText1 + Result2 ;
number3.setText(editText3 + "");
}
});
enter code here
The code works but displays with the E. Could you show me what code to use to simply get it to display the result as in decimal. The decimal result should be 11,000,000
No need to worry about commas at the moment.
Some languages use a Decimal data type which would take care of this problem I think. Anyone know why Android do not have this?
I looked at
http://lecturesnippets.com/android-variables-data-types/
which shows a list of the data types, but Double seems to be the biggest container and uses the Exponent thing I don't want.
Thanks for any help.
Al
You're looking for a DecimalFormat object. You want to pass your double into the DecimalFormat.format( ... ) method to get a StringBuffer, and then append the rest of the text you'd like to display to that StringBuffer before you pass it to your EditText.
You'll be particularly interested in this method:
public StringBuffer format (double value, StringBuffer buffer, FieldPosition position)
Writing my first android app. Almost everything works except this function. Even when all the text boxes have some numeric value, the app crashes in this function. Also, is there a better way to retrieve all the values in these text boxes and convert it to int. Note that all the textboxes are input type number on the activity.xml file.
public int updateclks()
{
try{
EditText v1= (EditText) findViewById(R.id.editText1);
EditText v2 = (EditText) findViewById(R.id.EditText01);
EditText v3 = (EditText) findViewById(R.id.EditText02);
EditText v4 = (EditText) findViewById(R.id.EditText03);
EditText c1= (EditText) findViewById(R.id.EditText04);
EditText c2 = (EditText) findViewById(R.id.EditText05);
EditText c3 = (EditText) findViewById(R.id.EditText06);
EditText c4 = (EditText) findViewById(R.id.EditText07);
try{ //error occurs somewhere around this block
vsel[1]= Integer.parseInt(v1.getText().toString());
vsel[2]= Integer.parseInt(v2.getText().toString());
vsel[3]= Integer.parseInt(v3.getText().toString());
vsel[4]= Integer.parseInt(v4.getText().toString());
clk[1]= Integer.parseInt(c1.getText().toString());
clk[2]= Integer.parseInt(c2.getText().toString());
clk[3]= Integer.parseInt(c3.getText().toString());
clk[4]= Integer.parseInt(c4.getText().toString());
}
catch(NumberFormatException e)
{
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "Fields cannot be empty", duration);
toast.show();
return 1;
}
return 0;
}
parseInt() is not crashing your app.Because if any exception occurs for this parseInt it will be caught as you handled it.Since your application is still crashing see vse1 and clk1 array whether you created these correctly or not.
Also notice about array length if you created vse1 array of length 4.Then You may want to start with index 0 rather than 1.Otherwise ArrayIndexOutOfBounds will crash your application.
I also got the same problem and u can solve it by using trim function.
clk[1]= Integer.parseInt(c1.getText().toString().trim());
I need to separate the input of an Edit Text on android, the input is in this format 4589, so I want to send the 45 to a list view, and the 89 to a Edit Text, somebody can help me I will appreciate it. thanks
The question is not clear. But you can try something like this
EditText et = (EditText) findViewById(R.id.editText);
String input = et.getText().toString();
String toEditText = input.substring(0,2); //45
String toListView = input.substring(2); //89
now you have the strings, use setText() to print
int a = Integer.parseInt(editText.getText());
int listNum = a / 100; //45
int editNum = a - listNum*100; //89
Here is a solution using integer division.
In my android app, I am getting the String from an Edit Text and using it as a parameter to call a web service and fetch JSON data.
Now, the method I use for getting the String value from Edit Text is like this :
final EditText edittext = (EditText) findViewById(R.id.search);
String k = edittext.getText().toString();
Now normally it works fine, but if we the text in Edit Text contains space then my app crashes.
for eg. - if someone types "food" in the Edit Text Box, then it's OK
but if somebody types "Indian food" it crashes.
How to remove spaces and get just the String ?
Isn't that just Java?
String k = edittext.getText().toString().replace(" ", "");
try this...
final EditText edittext = (EditText) findViewById(R.id.search);
String k = edittext.getText().toString();
String newData = k.replaceAll(" ", "%20");
and use "newData"
String email=recEmail.getText().toString().trim();
String password=recPassword.getText().toString().trim();
In the future, I highly recommend checking the Java String methods in the API. It's a lifeline to getting the most out of your Java environment.
You can easily remove all white spaces using something like this. But you'll face another serious problem if you just do that. For example if you have input
String input1 = "aa bb cc"; // output aabbcc
String input2 = "a abbcc"; // output aabbcc
String input3 = "aabb cc"; // output aabbcc
One solution will be to fix your application to accept white spaces in input string or use some other literal to replace the white spaces. If you are using only alphanumeric values you do something like this
String input1 = "aa bb cc"; // aa_bb_cc
String input2 = "a abbcc"; //a_abbcc
String input3 = "aabb cc"; //aabb_cc
And after all if you are don' caring about the loose of information you can use any approach you want.