Kersey
Kersey

Reputation: 31

How to display decimal value from firestore database to android studio

I'm trying to show the points from my firestore database unto my android application but it only shows whole number instead of actually including the decimal numbers. Firestore database: https://i.sstatic.net/UyexJ.jpg Android application: https://i.sstatic.net/zffM3.jpg

I have read previous posts about format("#.##") or DecimalFormat("#,###.####") but I'm not entirely sure how to apply that to my code.

public class MainActivity extends AppCompatActivity {
    private TextView powents;
    FirebaseFirestore fStore;
    FirebaseAuth fAuth;

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

        Button logout = findViewById(R.id.logout);
        ImageView qrimage = findViewById(R.id.qrimage);
        powents = findViewById(R.id.powents);
        fStore = FirebaseFirestore.getInstance();
        fAuth = FirebaseAuth.getInstance();

        FirebaseUser user = fAuth.getCurrentUser();
        if (user != null) {
            String mail = user.getEmail();
            DocumentReference docref = fStore.collection("Users").document(mail);
            docref.addSnapshotListener(this, (documentSnapshot, error) -> {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    powents.setText(String.valueOf(Math.toIntExact(documentSnapshot.getLong("Points"))));
                }
            });
        }
        try {
            BarcodeEncoder barcode = new BarcodeEncoder();
            Bitmap bitmap = barcode.encodeBitmap(Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getEmail(), BarcodeFormat.QR_CODE, 650, 650);
            qrimage.setImageBitmap(bitmap);
        } catch (Exception e) {
            e.printStackTrace();
        }

        logout.setOnClickListener(view -> {
            FirebaseAuth.getInstance().signOut();
            Toast.makeText(MainActivity.this, "Logged Out!", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(MainActivity.this, Login.class));
            finish();
        });
    }
}

Upvotes: 1

Views: 115

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317532

Your code is explicitly asking Firestore for a long (integer) value:

documentSnapshot.getLong("Points")

That's not what you want. You want a double (floating point) value instead using getDouble():

documentSnapshot.getDouble("Points")

You can then turn that into a string for your TextView.

Upvotes: 2

Related Questions