Big number calculating in Android without Exponent "E" - android

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)

Related

How to include £ symbol before the resulting SetText from a calculation?

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.

Converting TextView value to double variable

In front I set the text like that with the priceFormat being S$%.2f.
textPrice.setText(String.format(priceFormat, item.getPrice()));
Now I want to convert it to a double variable which I definitely think I have to make use of the priceFormat but I have no idea how to. This bottom line is wrong.
double Price=Double.parseDouble(textPrice.getText());
You need to convert the textPrice.getText() to a String since its Double.parseDouble(String):
double price = Double.parseDouble(mStatus.getText().toString());
You also have to eliminate the S$ and the trailing .:
double price = Double.parseDouble(mStatus.getText().toString().replaceAll("S\\$|\\.$", ""));
Of course you should make this less error-prone:
double price = 0d;
try {
price = Double.parseDouble(mStatus.getText().toString().replaceAll("S\\$|\\.$", ""));
}
catch (NumberFormatException e) {
// show an error message to the user
textPrice.setError("Please enter a valid number");
}
you need to remove that S$ before parsing, one of the way is:
String text = textPrice.getText();
String priceText = text.split("$")[1].trim(); //splitting numeric characters with the currency characters
double priceVal = Double.parseDouble(priceText); //parsing it to double

How Do I Create a Calculator To Add User Inputs From Same EditText?

I want to create a calculator only to add the user digit inputs given in same edittext and show in second edittext. Here is an example.
52 is entered by user in a EditText.
i want to perform addition in these number and show the result in second edittext.
answer should be 5+2=7.
i don't now what to perform
so i am performing this task.
int ans = a+b;
final int[] oil={ans};
final String str = String.valueOf(R.id.editText1);
final int y = Integer.parseInt(str);
final int z = oil[y];
et2.setText(z);
This is what you are expecting i think
String val=et1.getText().toString();
char[] valarry =val.toCharArray();
int result= Integer.parseInt(String.valueOf(valarry[0]))+Integer.parseInt(String.valueOf(valarry[1]));
et2.setText(result);

Android Counter Application Score List

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

Android - Getting User Input As An Integer

I'm a new Android developer. As a starting project, I'm trying to create a basic addition calculator. I have an EditText which is supposed to take the input (input is a string) and convert it to int1 when Button1 is pressed. When Button2 is pressed, it is supposed to take the input, convert it to int2, add int1 and int2 together and store the result in the int ans, and set the text of the EditText to ans. However, when I try to use Integer.parseInt(et.getText().toString()) I get an error and the app force closes. Could anyone provide me with the code to properly convert these Strings to integers? Thank you.
static int fn = 0;
static int sn = 0;
static int ans = 0;
static int pro = 0;
//"+" Button Clicked//
if(pro == 0){
fn = Integer.parseInt(entry.getText().toString());
entry.setText("");
pro++;
}else{
//MessageBox Crap//
//"=" Button Clicked//
sn = Integer.parseInt(entry.getText().toString());
ans = fn + sn;
entry.setText(ans);
Shouldn't ans be converted to a string before you set the contents of the EditText?

Categories

Resources