Getting Scientific notation in result of Calculator

Following is my Java code of Android Calculator app:

public abstract class Calculator extends Activity
implements OnTextSizeChangeListener, EvaluateCallback, OnLongClickListener {

private static final String NAME = Calculator.class.getName();

// instance state keys
private static final String KEY_CURRENT_STATE = NAME + "_currentState";
private static final String KEY_CURRENT_EXPRESSION = NAME + "_currentExpression";

/**
* Constant for an invalid resource id.
*/
public static final int INVALID_RES_ID = -1;

protected enum CalculatorState {
INPUT, EVALUATE, RESULT, ERROR
}

private final TextWatcher mFormulaTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
}

@Override
public void onTextChanged(CharSequence charSequence, int start, int count, int after) {
}

@Override
public void afterTextChanged(Editable editable) {
setState(CalculatorState.INPUT);
mEvaluator.evaluate(editable, Calculator.this);
}
};

private final OnKeyListener mFormulaOnKeyListener = new OnKeyListener() {
@Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
switch (keyCode) {
case KeyEvent.KEYCODE_NUMPAD_ENTER:
case KeyEvent.KEYCODE_ENTER:
if (keyEvent.getAction() == KeyEvent.ACTION_UP) {
onEquals();
}
// ignore all other actions
return true;
}
return false;
}
};

private final Editable.Factory mFormulaEditableFactory = new Editable.Factory() {
@Override
public Editable newEditable(CharSequence source) {
final boolean isEdited = mCurrentState == CalculatorState.INPUT
|| mCurrentState == CalculatorState.ERROR;
return new CalculatorExpressionBuilder(source, mTokenizer, isEdited);
}
};

private CalculatorState mCurrentState;
private CalculatorExpressionTokenizer mTokenizer;
private CalculatorExpressionEvaluator mEvaluator;

protected RelativeLayout mDisplayView;
protected CalculatorEditText mFormulaEditText;
protected CalculatorEditText mResultEditText;
private NineOldViewPager mPadViewPager;
private View mDeleteButton;
private View mClearButton;
private View mEqualButton;
private View mFactorialButton;
InterstitialAd mInterstitialAd;

public void showAds(){
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getString(R.string.inter));
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.setAdListener(new AdListener(){
@Override
public void onAdLoaded() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);

mDisplayView = (RelativeLayout) findViewById(R.id.display);
mFactorialButton = findViewById(R.id.factorial);
mFormulaEditText = (CalculatorEditText) findViewById(R.id.formula);
mResultEditText = (CalculatorEditText) findViewById(R.id.result);
mPadViewPager = (NineOldViewPager) findViewById(R.id.pad_pager);
mDeleteButton = findViewById(R.id.del);
final AdView adView = (AdView)findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
adView.setAdListener(new AdListener(){
@Override
public void onAdLoaded() {
if(adView.getVisibility()==View.GONE){
adView.setVisibility(View.VISIBLE);
}
}
});


mEqualButton = findViewById(R.id.pad_numeric).findViewById(R.id.eq);


mTokenizer = new CalculatorExpressionTokenizer(this);
mEvaluator = new CalculatorExpressionEvaluator(mTokenizer);

savedInstanceState = savedInstanceState == null ? Bundle.EMPTY : savedInstanceState;
setState(CalculatorState.values()[
savedInstanceState.getInt(KEY_CURRENT_STATE, CalculatorState.INPUT.ordinal())]);
String keyCurrentExpr = savedInstanceState.getString(KEY_CURRENT_EXPRESSION);
mFormulaEditText.setText(mTokenizer.getLocalizedExpression(
keyCurrentExpr == null ? "" : keyCurrentExpr));
mEvaluator.evaluate(mFormulaEditText.getText(), this);

mFormulaEditText.setEditableFactory(mFormulaEditableFactory);
mFormulaEditText.addTextChangedListener(mFormulaTextWatcher);
mFormulaEditText.setOnKeyListener(mFormulaOnKeyListener);
mFormulaEditText.setOnTextSizeChangeListener(this);
mDeleteButton.setOnLongClickListener(this);
}

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
// If there's an animation in progress, end it immediately to ensure the state is
// up-to-date before it is serialized.
cancelAnimation();

super.onSaveInstanceState(outState);

outState.putInt(KEY_CURRENT_STATE, mCurrentState.ordinal());
outState.putString(KEY_CURRENT_EXPRESSION,
mTokenizer.getNormalizedExpression(mFormulaEditText.getText().toString()));
}

protected void setState(CalculatorState state) {
if (mCurrentState != state) {
mCurrentState = state;

if (state == CalculatorState.RESULT || state == CalculatorState.ERROR) {
mDeleteButton.setVisibility(View.VISIBLE);
// mClearButton.setVisibility(View.VISIBLE);
} else {
mDeleteButton.setVisibility(View.VISIBLE);
// mClearButton.setVisibility(View.GONE);
}

if (state == CalculatorState.ERROR) {
final int errorColor = getResources().getColor(R.color.calculator_error_color);
mFormulaEditText.setTextColor(errorColor);
mResultEditText.setTextColor(errorColor);
Utils.setStatusBarColorCompat(getWindow(), errorColor);
} else {
mFormulaEditText.setTextColor(
getResources().getColor(R.color.display_formula_text_color));
mResultEditText.setTextColor(
getResources().getColor(R.color.display_result_text_color));
Utils.setStatusBarColorCompat(getWindow(), getResources().getColor(R.color.calculator_accent_color));
}
}
}



public void onButtonClick(View view) {
switch (view.getId()) {
case R.id.eq:
onEquals();
showAds();
break;
case R.id.clearBtn:
mFormulaEditText.setText("");
mResultEditText.setText("");
showAds();
break;
case R.id.del:
onDelete();
break;
case R.id.fun_cos:
case R.id.fun_ln:
case R.id.fun_log:
case R.id.fun_sin:
case R.id.fun_tan:
// Add left parenthesis after functions.
mFormulaEditText.append(((Button) view).getText() + "(");
break;
default:
mFormulaEditText.append(((Button) view).getText());
break;
}
}

@Override
public boolean onLongClick(View view) {
if (view.getId() == R.id.del) {
//onClear();
return true;
}
return false;
}

@Override
public void onEvaluate(String expr, String result, int errorResourceId) {
if (mCurrentState == CalculatorState.INPUT) {
mResultEditText.setText(result);
} else if (errorResourceId != INVALID_RES_ID) {
onError(errorResourceId);
} else if (!TextUtils.isEmpty(result)) {
onResult(result);
} else if (mCurrentState == CalculatorState.EVALUATE) {
// The current expression cannot be evaluated -> return to the input state.
setState(CalculatorState.INPUT);
}

mFormulaEditText.requestFocus();
}


private void onEquals() {
if (mCurrentState == CalculatorState.INPUT) {
setState(CalculatorState.EVALUATE);
mEvaluator.evaluate(mFormulaEditText.getText(), this);
}
}

What I want is to avoid Scientific Notation in my Calculator

Example: If I multiply the number 999999 x 66666, I should get the answer as 66665933334 but I am getting the answer as 6.6665933E10

Can anyone please help?

 

Hi @ashwint87,

This post was moved to a different board that fits your topic of discussion a bit better. This means you’ll get better engagement on your post, and it keeps our Community organized so users can more easily find information.

As you’ll notice, your Topic is now in the Project Development Help and Advice board. No action is needed on your part; you can continue the conversation as normal here.

Let me know if you have any other questions or if I can help with anything else.