Вопрос

TextView.setText(XMLdata.get("XMLField") + " mm");

input values that are expressed in an abbreviated way:

10,5858555966668 (with comma)

Which I would like to convert into:

10,58
Это было полезно?

Решение

if your XMLdata.get("XMLField") value is float u can do the following code

String finalValue = String.format(
                    "%.2f", XMLdata.get("XMLField"));

TextView.setText(finalValue + " mm");

if your XMLdata.get("XMLField") is String convert it into float do the above method.

Другие советы

TextView.setText((Math.floor(MathXMLdata.get("XMLField") * 100.0) / 100.0).toString() + " mm");

This converts 10,5858555966668 to 1058,58555966668, rounds it to 1058 and converts it back to 10,58.

Note: Math.floor always rounds down (cuts off all missing fractional numbers). If you intend to round to the closest value, use Math.round.

Thank you, thank you very much rmkrishna and Faro ((= I do it with

TextView.setText(String.format("%.2f",Double.parseDouble(XMLData.get("Son7GunlukSagis").replace(",", ".")))+ " mm");

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top