-3

I am fairly new to programming, i have a method, which gives out the float values. It shows a lot of values after the decimal point. I simply need to show the value which has NO decimal. It should be a rounded off value.

Have a look at my code:

@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 = roundFirstDecimal(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) {
            float timeOver = (System.currentTimeMillis()
                    - startTime);
            tf.setText(String.valueOf(pointAverage / (timeOver / 1000)));

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

    }

How can i do it, i tried googling any video tutorial that could help but failed so posting my problem here.

Thankyou

  • 1
    so, if I understand your issue correctly, you could not find a way in java to round a value? – njzk2 Apr 20 '15 at 17:25
  • 2
    Is google not working in your country? http://developer.android.com/reference/java/lang/Math.html#round(double) – Phantômaxx Apr 20 '15 at 17:25
  • Seems to be a duplicate of http://meta.stackexchange.com/questions/222027/what-happened-to-stack-overflow-i-cant-access-it-when-will-it-be-working – 323go Apr 20 '15 at 17:30

1 Answers1

-2

Just call Math.round(float). Check out the Javadocs for the Math class.

Assisting
  • 16
  • 3
  • 2
    Even casting to an int might be enough. – Stan Apr 20 '15 at 17:28
  • 1
    @Stan Last I checked that just truncates the value rather than rounding properly. So 5.9 casts to 5. – Assisting Apr 20 '15 at 17:29
  • And where do i call math.round(float) in my code? – Mohsin Aslam Apr 20 '15 at 17:30
  • It depends, exactly which set of decimals are annoying you? For instance, if it was `tvd.setText(nMaxSpeed.toString());` you'd change it to `tvd.setText(Math.round(nMaxSpeed).toString());` – Assisting Apr 20 '15 at 17:35
  • Yeah, but asker didn't ask for rounding :) – Stan Apr 20 '15 at 17:35
  • Strictly speaking they did somewhat ask for two different things ("NO decimal", then "rounded off value"), so I went with the answer that would result in more obvious behaviour. – Assisting Apr 20 '15 at 17:38
  • float timeOver = (System.currentTimeMillis() - startTime); tf.setText(String.valueOf(pointAverage / (timeOver / 1000))); – Mohsin Aslam Apr 20 '15 at 17:38
  • This part dont get solved no matter what i try to do – Mohsin Aslam Apr 20 '15 at 17:39
  • Oh, I see. Then you'll need to change it to `tf.setText(String.valueOf(Math.round(pointAverage / (timeOver / 1000))));` But please, take a moment to think about why this would be the case, because it's the sort of thing you'll need to do a lot of in your programming lifetime. – Assisting Apr 20 '15 at 17:41
  • Worked but last thing bro, when i try to call it with maxspeed and nCurrent Speed so it says "The method round(double) in the type Math is not applicable for the arguments (String)" – Mohsin Aslam Apr 20 '15 at 17:57
  • tvd.setText(Math.round(nMaxSpeed).toString()); Isn't working here Error: Cannot invoke toString() on the primitive type int – Mohsin Aslam Apr 20 '15 at 18:03
  • Here are some leading questions to help you figure out the answers: 1) You can't use any rounding methods on Strings because they're treated as "words", not "numbers". So, where should you use the `round()` method instead it? 2) When you call `toString()` on the integer, it doesn't work. Where else in the code have you tried to get the valueOf a String? Can you repeat the process here? – Assisting Apr 20 '15 at 19:23