Android examples for java.lang:Float
format Float by position
import android.text.Spannable; import android.text.SpannableStringBuilder; import android.text.style.ForegroundColorSpan; import java.text.DecimalFormat; public class Main{ public static String formatFloat(float f, int pos) { float p = 1f; StringBuilder format = new StringBuilder("#0"); for (int i = 0; i < pos; i++) { if (i == 0) { format.append('.'); }/*from w w w . jav a 2 s .c om*/ p *= 10f; format.append('0'); } f = Math.round(f * p) / p; DecimalFormat formatter = new DecimalFormat(format.toString()); return formatter.format(f); } }