Format Float to n decimal places
Solution 1
You may also pass the float value, and use:
String.format("%.2f", floatValue);
Solution 2
Take a look at DecimalFormat. You can easily use it to take a number and give it a set number of decimal places.
Edit: Example
Solution 3
Try this this helped me a lot
BigDecimal roundfinalPrice = new BigDecimal(5652.25622f).setScale(2,BigDecimal.ROUND_HALF_UP);
Result will be roundfinalPrice --> 5652.26
Solution 4
Of note, use of DecimalFormat
constructor is discouraged. The javadoc for this class states:
In general, do not call the DecimalFormat constructors directly, since the NumberFormat factory methods may return subclasses other than DecimalFormat.
https://docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html
So what you need to do is (for instance):
NumberFormat formatter = NumberFormat.getInstance(Locale.US);
formatter.setMaximumFractionDigits(2);
formatter.setMinimumFractionDigits(2);
formatter.setRoundingMode(RoundingMode.HALF_UP);
Float formatedFloat = new Float(formatter.format(floatValue));
Solution 5
Here's a quick sample using the DecimalFormat
class mentioned by Nick.
float f = 12.345f;
DecimalFormat df = new DecimalFormat("#.00");
System.out.println(df.format(f));
The output of the print statement will be 12.35. Notice that it will round it for you.
Related videos on Youtube
seba123neo
Android Developer VB6, VB .NET, C#, Java Amateur Astronomer and Astrophotographer
Updated on July 08, 2022Comments
-
seba123neo almost 2 years
I need to format a float to "n"decimal places.
was trying to BigDecimal, but the return value is not correct...
public static float Redondear(float pNumero, int pCantidadDecimales) { // the function is call with the values Redondear(625.3f, 2) BigDecimal value = new BigDecimal(pNumero); value = value.setScale(pCantidadDecimales, RoundingMode.HALF_EVEN); // here the value is correct (625.30) return value.floatValue(); // but here the values is 625.3 }
I need to return a float value with the number of decimal places that I specify.
I need
Float
value return notDouble
.
-
Zach L about 13 years+1. Here's the Android-specific documentation,which should be essentially the same.
-
seba123neo about 13 yearsbut I need the result is a float number, not a string.
-
seba123neo about 13 yearsbut I need the result is a float number, not a string.
-
Nick Campion about 13 yearsAre you saying you want to round the float? Normally the only time precision of a float matters is for display.
-
n3utrino about 13 yearsif you need a float to show it to the user, there may be something wrong with the design of the application. Usually the float is used to calculate and a string is used to show it to the user.
-
seba123neo about 13 yearssorry, you're right, I was confused, just format the number when shown on screen, not before, that was my question, thank you very much, problem solved.
-
seba123neo about 13 yearssorry, you're right, I was confused, just format the number when shown on screen, not before, that was my question, thank you very much, problem solved.
-
Gerard almost 11 yearsThis is more useful for those looking to round and do some further computations with it, otherwise, Arve's answer is probably best.
-
Gomino about 8 yearsPlease be carefull as String.format depend on your current Local configuration, you may not get a dot as a separator. Prefer using
String.format(java.util.Locale.US,"%.2f", floatValue);
-
FBB almost 8 yearsOf note, use of
DecimalFormat
constructor is discouraged, see my answer for a correct use ofNumberFormat
. -
FBB almost 8 yearsOf note, use of
DecimalFormat
constructor is discouraged, see my answer for a correct use ofNumberFormat
. -
ZhaoGang about 7 yearsthis answer gives a String value, not a Float value, I don't think a good answer.
-
Mark Buikema about 7 yearsWhy would you force a dot as separator?
-
xeruf over 6 yearsI've also used this method for a while, but I would think that
pow
is a too expensive operation to use for rounding? -
Robin Davies over 6 yearsNever optimize until you actually have a performance problem. More important to be right than fast. And I can't honestly imagine an application where performance of controlled rounding would be an issue, unless you were Visa or Mastercard (who would just throw more machines at it). And pow() is probably faster than round().
-
Tjaart over 6 years@MarkBuikema I think there are cases where it would be absolutely necessary, such as when compiling document formats driven by standards, eg. PDF documents.
-
Shiva Krishna Chippa about 6 yearsThis is not working when (for example) floatValue = 8.499. It is giving 8.5.
-
FBB almost 6 years@ShivakrishnaChippa: yes, because here we specify that we want 2 digits after the comma, and to do a up rounding. In that case, 8.499 is rounded to 8.5. If you want 8.499 to be display, simply set 3 as the maximum fraction digits.
-
AlexV over 5 yearsThis works with GWT as well. String.format() - does not.
-
NateS over 2 yearsDecimalFormat#format takes a double, making the formatted float have more digits than a float can hold.
-
VanessaF about 2 yearsThis answer does not create a float