Float Precision Display (Android)

I am trying to make a program that requires some user input, performs a few calculations and prints the answer. My problem is that this answer sometimes lasts a lot of decimals, which causes some aesthetic and layout problems. I need to display only 4 decimal places. In any case, to limit the accuracy of these numbers to the output? (The numbers are stored in floats, and I'm programming for Android.)

+6
android floating-point precision
source share
1 answer

You can format the float up to 4 decimal places using String.format .

Example:

 String result = String.format("%.4f", theNumber); 

See also:

+25
source share

All Articles