You can use String.format("%.2f", d), your double will be rounded automatically.

Answer from OleGG on Stack Overflow
🌐
IQCode
iqcode.com › code › other › android-round-double-to-2-decimal
android round double to 2 decimal Code Example
September 11, 2021 - how to convert a decimal into a 2 decimal places in android android round to 1 decimal place java android convert double to two decimal spaces android java 2 decimal places float round up to 2 decimal places android android round to 2 decimal places round of after one digit decimal android get round number with 2 digits android android round float to 2 decimal places android studio round float to 2 decimal places how to fix a double to 2 decimal places in android android double value decimal places round up with 2 decimal in android studio convert to double with two decimal places in java andr
🌐
Android Developers
developer.android.com › api reference › decimalformat
DecimalFormat | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
Find elsewhere
🌐
Codeinfopark
codeinfopark.com › questions › android-round-double-to-2-decimal-places
Loading...
March 5, 2022 - We cannot provide a description for this page right now
Top answer
1 of 7
177

I was working with statistics in Java 2 years ago and I still got the codes of a function that allows you to round a number to the number of decimals that you want. Now you need two, but maybe you would like to try with 3 to compare results, and this function gives you this freedom.

/**
* Round to certain number of decimals
* 
* @param d
* @param decimalPlace
* @return
*/
public static float round(float d, int decimalPlace) {
    BigDecimal bd = new BigDecimal(Float.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
    return bd.floatValue();
}

You need to decide if you want to round up or down. In my sample code I am rounding up.

Hope it helps.

EDIT

If you want to preserve the number of decimals when they are zero (I guess it is just for displaying to the user) you just have to change the function type from float to BigDecimal, like this:

public static BigDecimal round(float d, int decimalPlace) {
    BigDecimal bd = new BigDecimal(Float.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);       
    return bd;
}

And then call the function this way:

float x = 2.3f;
BigDecimal result;
result=round(x,2);
System.out.println(result);

This will print:

2.30
2 of 7
58

Let's test 3 methods:
1)

public static double round1(double value, int scale) {
    return Math.round(value * Math.pow(10, scale)) / Math.pow(10, scale);
}

2)

public static float round2(float number, int scale) {
    int pow = 10;
    for (int i = 1; i < scale; i++)
        pow *= 10;
    float tmp = number * pow;
    return ( (float) ( (int) ((tmp - (int) tmp) >= 0.5f ? tmp + 1 : tmp) ) ) / pow;
}

3)

public static float round3(float d, int decimalPlace) {
    return BigDecimal.valueOf(d).setScale(decimalPlace, BigDecimal.ROUND_HALF_UP).floatValue();
}



Number is 0.23453f
We'll test 100,000 iterations each method.

Results:
Time 1 - 18 ms
Time 2 - 1 ms
Time 3 - 378 ms


Tested on laptop
Intel i3-3310M CPU 2.4GHz

🌐
Sharpfutures
sharpfutures.org.uk › REpVhsTw › android-round-double-to-2-decimal-places
android round double to 2 decimal places - SharpFutures
If your salary increases at an average annual rate of 7.83 percent, how long will it take to reach your goal?Round the answer to two decimal places.Your Answer: For example i) 12.10 to be displayed as 12.1, ii) 12.00 to be displayed as 12. You should write 100.0 not 100, otherwise it will treat it as int. android studio - Rounding double to 2 decimal places in an xml file with databinding - Stack Overflow Rounding double to 2 decimal places in an xml file with databinding Ask Question Asked 1 year ago Modified 1 year ago Viewed 834 times 0 <TextView android:text="@ {'' + String.valueOf (cartItem.product.price * cartItem.quantity)}" /> Why is the article "the" used in "He invented THE slide rule"?
🌐
Intellipaat
intellipaat.com › home › blog › how to round to two decimal places in java?
How to round to two decimal places in java?
February 26, 2026 - ... The Math.round() method rounds a floating-point number to the nearest whole number (long or int). For two decimal places, multiply by 100, round, and divide by 100. ... Yes, you can learn Java basics in 21 days with consistent practice.