Val Okafor
Val Okafor

Reputation: 3457

Convert from Double to String in Android

I am trying to convert a value from Double to String in an Android Activity.I can get this to work with my first example here below (working in the sense of no squigly error from Eclipse). However I am curious as to why the second example is not working.

First example

balance = (TextView) findViewById(R.id.textViewCardBalance);
Intent intent = getIntent();
if (intent.getExtras() != null) {                           
    balance.setText(String.valueOf((long)intent.getDoubleExtra("balance", 0.00)));
}

Second example below not working (Error: "Cannot cast from Double to Long"

balance = (TextView) findViewById(R.id.textViewCardBalance);
Double cardBalance;

Intent intent = getIntent();

if (intent.getExtras() != null) {
    cardBalance = intent.getDoubleExtra("balance", 0.00);
    balance.setText(String.valueOf((long)cardBalance);  
}

Would anyone know how I can get the second example to work as I need to log the value retrieved from the intent before passing it to the TextView.

Thanks

Upvotes: 0

Views: 1081

Answers (3)

Edwin Torres
Edwin Torres

Reputation: 2864

Why can't you do this?

balance.setText(cardBalance + "");

Upvotes: 4

victorvictord
victorvictord

Reputation: 56

using String v = ""+String.valueOf((long)cardBalance) not work?

Upvotes: 0

Zoran
Zoran

Reputation: 1494

String yourDoubleString = String.valueOf(yourDouble);

in your case:

String yourDoubleString = String.valueOf(intent.getDoubleExtra("balance", 0.00));

Upvotes: 1

Related Questions