Casey Casey - 5 months ago 37
Android Question

How do I take the text from a TextView and format it for USD Currency?

This is what im doing -->



//get entered texts from the edittexts,and convert to integers.
Integer value1 = Integer.parseInt(sand1.getText().toString());


//doing a calculation
Double calculatedValue1 = (9.5*value1);


//set the value to the textview, to display on screen.
result1.setText("$"+ calculatedValue1.toString());





result1 in the TextView looks like this now: ##.#

I need it to look like $##.##

Answer

Change your code to something like this :

/get entered texts from the edittexts,and convert to integers.
Integer value1 = Integer.parseInt(sand1.getText().toString());


//doing a calculation
Double calculatedValue1 = (9.5*value1);


//set the value to the textview, to display on screen.
result1.setText("$"+ String.format( "%.2f", calculatedValue1 ));