How to print a double with two decimals in Android?
yourTextView.setText(String.format("Value of a: %.2f", a));
How to print a float with 2 decimal places in Java?
You can use the printf
method, like so:
System.out.printf("%.2f", val);
In short, the %.2f
syntax tells Java to return your variable (val
) with 2 decimal places (.2
) in decimal representation of a floating-point number (f
) from the start of the format specifier (%
).
There are other conversion characters you can use besides f
:
d
: decimal integero
: octal integere
: floating-point in scientific notation
Android - Round to 2 decimal places
You can use String.format("%.2f", d)
, your double will be rounded automatically.
Rounding double to 2 decimal places in an xml file with databinding
Fixed by creating a private String total; inside CartItem
Created getter and setter for total
In the getter:
public String getTotal() {
return String.format("%.2f", quantity*product.getPrice());
}
then went to the xml file and simply put in the string total instead of doing the calculation inside of the xml file.
<TextView
android:text="@{'£' + cartItem.total}" />
How do I convert a double to 2 decimal places?
Remember you can't use
String.format("%.2f", maltRequiredString);
Because maltRequiredString is string. Correct coding is that you must use float in this function and for that you have to convert your string to float
float f = Float.valueOf(maltRequiredString);
String test = String.format("%.02f", f);
You can also use this technique for making it 2 decimal
DecimalFormat decimalFormat = new DecimalFormat("#.##");
float twoDigitsF = Float.valueOf(decimalFormat.format(f));
How do I format a double into a string (with only two decimal places)?
Use this code
fluidPerHourResultLabel.setText(new DecimalFormat("##.##").format(fluidHourInt));
Please check the link
Show only two digit after decimal
Round Double to 1 decimal place kotlin: from 0.044999 to 0.1
Finally I did what Andy Turner
suggested, rounded to 3 decimals, then to 2 and then to 1:
Answer 1:
val number:Double = 0.0449999
val number3digits:Double = String.format("%.3f", number).toDouble()
val number2digits:Double = String.format("%.2f", number3digits).toDouble()
val solution:Double = String.format("%.1f", number2digits).toDouble()
Answer 2:
val number:Double = 0.0449999
val number3digits:Double = Math.round(number * 1000.0) / 1000.0
val number2digits:Double = Math.round(number3digits * 100.0) / 100.0
val solution:Double = Math.round(number2digits * 10.0) / 10.0
Result:
0.045 → 0.05 → 0.1
Note: I know it is not how it should work but sometimes you need to round up taking into account all decimals for some special cases so maybe someone finds this useful.
Java: double: how to ALWAYS show two decimal digits
You can use something like this:
double d = 1.234567;
DecimalFormat df = new DecimalFormat("#.00");
System.out.print(df.format(d));
Edited to actually answer the question because I needed the real answer and this came up on google and someone marked it as the answer despite the fact that this wasn't going to work when the decimals were 0.
how to format string to show two decimal places
You can use String.format("%.2f", d)
, your double will be rounded automatically
pieChart.setCenterText("$ " + String.format("%.2f", d));
Related Topics
Error:Execution Failed for Task ':App:Kaptdebugkotlin'
Android: Runonuithread Vs Asynctask
Change The Background Color of Cardview Programmatically
How to Debug on a Real Device (Using Eclipse/Adt)
Viewpager Update Fragment on Swipe
How to Play .Mp4 Video in Videoview in Android
How to Print a Double with Two Decimals in Android
How to Get Pid of Android Application Without Using Adb Shell
Switch Back/Front Camera on Fly
Can't Create New Kotlin Project After Updating to Android Studio 4.2
How to Prevent The Activity from Loading Twice on Pressing The Button
How to Get Results from an Intentservice Back into an Activity
How to Click a Button Programmatically for a Predefined Intent
How to Find Out Carrier's Name in Android
How to Be Notified When a Snackbar Has Dismissed Itself
Sqlite Insert into Table Select * From