Pregunta

First of all please excuse me for my bad English speaking.

I am new to Android Development. I have a problem and think you can solve it.

The problem is:

I have a very big double value like 12345678987654321 in my android app but when i want to show it on EditText, it will be shown like this 12345678987654300. In this case when my value characters is over than 15 chars android shows remaining chars with "0"

i don't know what i have to to do.

i am using this code:

DecimalFormat df = new DecimalFormat("#.#########");
double a = Distancevals[1] * Distancevals[2];
//Distancevals is an array of double with big values
EditText editto = (EditText)findViewById(...);
editto.setText(df.format(a));
¿Fue útil?

Solución

Double stores your number 12345678987654321 in format 1.23456789876543E16, so you lose the end of the number 21. When you format the result it's known that your number consists of 17 signs, so format function adds two zeros in the end of your number instead of 21.

Try to use this:

DecimalFormat df = new DecimalFormat("#.#########");
BigDecimal a = new BigDecimal(Distancevals[1] * Distancevals[2]);
// Distancevals is an array of double with big values
EditText editto = (EditText) findViewById(...);
editto.setText(df.format(a));

Otros consejos

Try this one

DecimalFormat df= new DecimalFormat("###############00");
double a=Distancevals[1]*Distancevals[2];
//Distancevals is an array of double with big values
EditText editto=(EditText)findViewById(...);
editto.setText(df.format(a));

You just show it as editto.setText(a+"");

Cause in your case EditText is not doing anything but DecimalFormat is changing your number.

I suggest that in the line:

editto.setText(df.format(a));

Change it to:

editto.setText(df.format(a.toString()));
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top