How do I format a number in Java?
From this thread, there are different ways to do this:
double r = 5.1234;
System.out.println(r); // r is 5.1234
int decimalPlaces = 2;
BigDecimal bd = new BigDecimal(r);
// setScale is immutable
bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);
r = bd.doubleValue();
System.out.println(r); // r is 5.12
f = (float) (Math.round(n*100.0f)/100.0f);
DecimalFormat df2 = new DecimalFormat( "#,###,###,##0.00" );
double dd = 100.2397;
double dd2dec = new Double(df2.format(dd)).doubleValue();
// The value of dd2dec will be 100.24
The DecimalFormat() seems to be the most dynamic way to do it, and it is also very easy to understand when reading others code.
How to format a number 0..9 to display with 2 digits (it's NOT a date)
You can use:
String.format("%02d", myNumber)
See also the javadocs
Java: Format Numbers with commas
You can use NumberFormat
:
int n = 100000;
String formatted = NumberFormat.getInstance().format(n);
System.out.println(formatted);
Output:
100,000
How to format decimals in a currency format?
I doubt it. The problem is that 100 is never 100 if it's a float, it's normally 99.9999999999 or 100.0000001 or something like that.
If you do want to format it that way, you have to define an epsilon, that is, a maximum distance from an integer number, and use integer formatting if the difference is smaller, and a float otherwise.
Something like this would do the trick:
public String formatDecimal(float number) {
float epsilon = 0.004f; // 4 tenths of a cent
if (Math.abs(Math.round(number) - number) < epsilon) {
return String.format("%10.0f", number); // sdb
} else {
return String.format("%10.2f", number); // dj_segfault
}
}
How to format numbers to same number of digits, 0-padded?
Use String.format
:
Integer i = Integer.valueOf(src);
String format = "%1$07d";
String result = String.format(format, i);
How can I format a String number to have commas and round?
You might want to look at the DecimalFormat
class; it supports different locales (eg: in some countries that would get formatted as 1.000.500.000,57
instead).
You also need to convert that string into a number, this can be done with:
double amount = Double.parseDouble(number);
Code sample:
String number = "1000500000.574";
double amount = Double.parseDouble(number);
DecimalFormat formatter = new DecimalFormat("#,###.00");
System.out.println(formatter.format(amount));
Related Topics
Type List VS Type Arraylist in Java
When Should I Use "This" in a Class
Compare Two Objects With .Equals() and == Operator
Capturing Stdout When Calling Runtime.Exec
How to Add to List≪? Extends Number≫ Data Structures
What Is the Concept of Erasure in Generics in Java
Error: the Processing Instruction Target Matching "[Xx][Mm][Ll]" Is Not Allowed
Close a Scanner Linked to System.In
Difference Between a Static and a Non-Static Initialization Code Block
Difference Between Final and Effectively Final
What Does Servletcontext.Getrealpath("/") Mean and When Should I Use It
What Are the Rules For Evaluation Order in Java
How to Enter Quotes in a Java String
Including Dependencies in a Jar With Maven
Is There an Advantage to Use a Synchronized Method Instead of a Synchronized Block