-2

I'm working in Eclipse and using float for my variables and values. The only issue I am getting is the huge number of digits (1.563524354) after the decimal. Can I somehow limit them to one digit after the decimal like (1.5) or no digit or decimal at all simply like 1. ?

My code:

float nCurrentSpeed;
    private long startTime;
    private int count;
    private int pointAverage;
    private Float nMaxSpeed = 0F;
    DecimalFormat df = new DecimalFormat("#");


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LocationManager lm = (LocationManager) this
                .getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

        this.onLocationChanged(null);

    }

    @Override
    public void onLocationChanged(Location location) {
        TextView tv = (TextView) findViewById(R.id.textView1);
        TextView tvd = (TextView) findViewById(R.id.textView5);

        if (location == null) {
            startTime = System.currentTimeMillis();
            count = 0;
            pointAverage = 0;

            actualizeTextField();

            tv.setText("0.0");
        } else {

            nCurrentSpeed = location.getSpeed();
            tv.setText(nCurrentSpeed + "");
            count += 1;
            pointAverage += location.getSpeed();
            actualizeTextField();

            if (nMaxSpeed < nCurrentSpeed) {
                nMaxSpeed = nCurrentSpeed;
            }

        }
        tvd.setText(nMaxSpeed.toString());
    }

    private void actualizeTextField() {
        // TODO Auto-generated method stub
        TextView tf = (TextView) findViewById(R.id.textView3);

        if (count > 0) {
            long timeOver = System.currentTimeMillis() - startTime;
            tf.setText(String.valueOf(pointAverage / (timeOver / 1000)));
        } else {
            tf.setText("0.0");
        }
    }
Aleem Ahmed
  • 189
  • 1
  • 1
  • 11
  • 1
    possible duplicate of [Format Float to n decimal places](http://stackoverflow.com/questions/5195837/format-float-to-n-decimal-places) – user2340612 Apr 19 '15 at 14:41

3 Answers3

0
DecimalFormat df = new DecimalFormat("#.#");
df.format(0.91231);

returns:

0.9

If you don't want decimal places, why don't you cast it to int then?

poss
  • 1,759
  • 1
  • 12
  • 27
0

Like this :

double d = 1.234567;

DecimalFormat df1 = new DecimalFormat("#.##"); // set your format

System.out.print(df1.format(d))

Shahbaz Hashmi
  • 2,631
  • 2
  • 26
  • 49
0

If you don't need point values, why are you using float? Check this one.

float nCurrentSpeed;
private long startTime;
private int count;
private int pointAverage;
private int nMaxSpeed = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LocationManager lm = (LocationManager) this
            .getSystemService(Context.LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

    this.onLocationChanged(null);

}

@Override
public void onLocationChanged(Location location) {
    TextView tv = (TextView) findViewById(R.id.textView1);
    TextView tvd = (TextView) findViewById(R.id.textView5);

    if (location == null) {
        startTime = System.currentTimeMillis();
        count = 0;
        pointAverage = 0;

        actualizeTextField();

        tv.setText("0.0");
    } else {

        nCurrentSpeed = location.getSpeed();
        tv.setText(nCurrentSpeed + "");
        count += 1;
        pointAverage += location.getSpeed();
        actualizeTextField();

        if (nMaxSpeed < nCurrentSpeed) {
            nMaxSpeed = (int)nCurrentSpeed;
        }

    }
    tvd.setText(String.valueOf(nMaxSpeed));
}

private void actualizeTextField() {
    // TODO Auto-generated method stub
    TextView tf = (TextView) findViewById(R.id.textView3);

    if (count > 0) {
        long timeOver = System.currentTimeMillis() - startTime;
        tf.setText(String.valueOf(pointAverage / (timeOver / 1000)));
    } else {
        tf.setText("0.0");
    }
}
Shahbaz Hashmi
  • 2,631
  • 2
  • 26
  • 49