如何限制浮点数在小数点后给出一个值或没有值

时间:2015-04-19 14:38:54

标签: eclipse

我在Eclipse中工作并使用float作为我的变量和值。我得到的唯一问题是小数点后的大量数字(1.563524354)。我可以以某种方式将它们限制在十进制之后的一位数,如(1.5)或根本没有数字或十进制,就像1.?

我的代码:

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");
        }
    }

3 个答案:

答案 0 :(得分:0)

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

返回:

0.9

如果您不想要小数位,那么为什么不将它转换为int呢?

答案 1 :(得分:0)

像这样:

双d = 1.234567;

DecimalFormat df1 = new DecimalFormat(&#34;#。##&#34;); //设置格式

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

答案 2 :(得分:0)

如果您不需要点值,为什么使用浮点数?检查一下。

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");
    }
}