How to display or print fixed number of decimal points of float data in textView of Android App?

Published: 25 April 2024
on channel: Programmer World
115
8

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

--


Watch video How to display or print fixed number of decimal points of float data in textView of Android App? online without registration, duration hours minute second in high quality. This video was added by user Programmer World 25 April 2024, don't forget to share it with your friends and acquaintances, it has been viewed on our site 115 once and liked it 8 people.