Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using String.format() string in a toast Android

I am trying to figure out how to input a formatted string to use in a toast. The toast works with a plain old string, such as

string s1 = "You scored"

But it will not work with String.format(). Why is this? Is there any way to work around this? I already know it will work if I do

Toast.makeText(this, "You scored: " + score + "%", Toast.LENGTH_LONG).show();

But I want the percentage to not have any decimal numbers. I have searched around but could not find an answer.

This is what doesn't work:

float score = ((float) mNumberCorrect / 6)*100;

Toast.makeText(this, String.format("You scored: %d", score) , Toast.LENGTH_LONG).show();
like image 481
Bryant Avatar asked Dec 12 '25 05:12

Bryant


1 Answers

you have to write %f in place of %d like below.

float score = ((float) mNumberCorrect / 6)*100;

Toast.makeText(this, String.format("You scored: %f", score) , Toast.LENGTH_LONG).show();
like image 50
Mehul Kabaria Avatar answered Dec 14 '25 17:12

Mehul Kabaria



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!