wyoskibum
wyoskibum

Reputation: 1859

Android string formatting

I have a textview that I'm setting the text from a value obtained from a SimpleCursorAdapter. The field in my SQLite database is a REAL number. Here is my code:

        // Create the idno textview with background image
        TextView idno = (TextView) view.findViewById(R.id.idno);
        idno.setText(cursor.getString(3));

My issue is that the text display a decimal. The value is 1081, but I'm getting 1081.0000. How can I convert the string to not display the decimals? I've looked into the formatter, but I can't get the syntax right.

        TextView idno = (TextView) view.findViewById(R.id.idno);
        String idno = cursor.getString(3);
        idno.format("@f4.0");
        idno.setText(idno);

Thanks in advanced!

Upvotes: 0

Views: 4036

Answers (2)

Jason Robinson
Jason Robinson

Reputation: 31283

You can use String.format:

String idno = String.format("%1$.0f", cursor.getDouble(3));

Can also you DecimalFormat:

DecimalFormat df = new DecimalFormat("#");
String idno = df.format(cursor.getDouble(3));

Upvotes: 2

MByD
MByD

Reputation: 137322

If you get a String with a decimal point, you can simply do:

idno.setText(cursor.getString(3).split("\\.")[0]);
//          Split where there is a point--^   ^
//                                            |
//          Get the first in the array--------+

Note that this:

TextView idno = (TextView) view.findViewById(R.id.idno);
String idno = cursor.getString(3);

is illegal, as you use the same variable name.

Upvotes: 0

Related Questions