In this video it shows how to limit the number of decimal points of a float or decimal while printing it on a textView widget of Android App. It basically uses below line of code to limit the decimal places.
String stringFormatted = String.format("%.03f", floatData);
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: [email protected]
Complete source code and other details/ steps of this video are posted in the below link:
https://programmerworld.co/android/ho...
However, the main Java code is copied below also for reference:
package com.programmerworld.fixeddecimalpointsinfloat;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
private Float aFloatNumber = .621478f;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) - {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
textView = findViewById(R.id.textView);
textView.setText(String.valueOf(aFloatNumber));
return insets;
});
}
public void buttonFixDecimalPoints(View view){
textView.setText(String.format("%.03f", aFloatNumber));
}
}
--
Смотрите видео How to display or print fixed number of decimal points of float data in textView of Android App? онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Programmer World 25 Апрель 2024, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 115 раз и оно понравилось 8 людям.