I'm using DecimalFormat and need to add thousands separator to the decimal places of numbers. For example, I need to:
DecimalFormat fmt = new DecimalFormat("#,##0.0##_#"); // ?
System.out.println(fmt.format(1234.0)); // 1,234.0
System.out.println(fmt.format(0.1201)); // 0.120_1
System.out.println(fmt.format(0.00001234)); // 0.000_012_34
How do I specify the format for the decimal places?
Related
I'm trying to to format a number using the DecimalFormat but the problem that I didn't get the expected result. Here is the problem:
I have this number: 1439131519 and I want to print only the five first digits but with a comma after 4 digits like this: 1439,1. I have tried to use DecimalFormat but it didn't work.
I tried like this but it dosen't work:
public static DecimalFormat format2 = new DecimalFormat("0000.0");
Anyone have an idea?
It is easier to do with maths rather than formatting.
Assuming your number is in a double:
double d = 1439131519;
d = d / 100000; // d = 14391,31519
d = Math.round(d) // d = 14391
d = d / 10; // d = 1439,1
Of course, you can do it in one line of code if you want. In using Math.round I am assuming you want to round to the nearest value. If you want to round down you can use Math.floor.
The comma is the normal decimal separator in much of Europe, so that might work by default in your locale. If not, you can force it by getting the formatter for a locale such as Germany. See: How to change the decimal separator of DecimalFormat from comma to dot/point?.
I've already tried a way to format this whole number in decimal plus no solve the one that comes closest to the result hoping was using the BigDecimal plus it was very extensive. Where am I going wrong?
Double value = 20852;
DecimalFormat df = new DecimalFormat("0.00##");
String result = df.format(value);
result = 20852.00
expected outcome : 20.852
With decimal format you define how many decimal places are need to be set. The output is completely correct. It would create the desired output if you have double value = 20.852. What you want is to set the thousand sepeator:
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
DecimalFormat df = new DecimalFormat("###,###.##", symbols);
As stated by your question, without changing anything to your number and to get the result your expressively required, you could do this with setting the dot character as a thousands separator :
Double value = 20852d;
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
symbols.setGroupingSeparator('.');
DecimalFormat df = new DecimalFormat("##,000", symbols);
String result = df.format(value);
Double.valueOf with comma decimal separator throws NumberFormatException.
Java 1.7.0_67 and 1.8.0_25.
I also try to set DecimalFormatSymbols with "," as decimalSeparator.
Locale.setDefault(Locale.FRANCE);
assert "12,3".equals(NumberFormat.getInstance().format(12.3));
if (((DecimalFormat) NumberFormat.getInstance()).getDecimalFormatSymbols().getDecimalSeparator() == ',')
Double.valueOf("12,3");
NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
Number number = format.parse("12,3");
Double d = number.doubleValue();
System.out.println(d);
Double.valueOf() is not Locale aware. It only understands numbers with dots as decimal places.
Luckily you can use the same NumberFormat instance for formatting and parsing which is Locale aware ....
NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
System.out.println(format.format(12.3)); // ==> "12,3"
System.out.println(format.parse("12,3")); // ==> 12.3
Double.valueOf(String) does not take the default locale into account. Note that the API documentation of the method explains exactly what format it expects. It also tells you this:
To interpret localized string representations of a floating-point value, use subclasses of NumberFormat.
Use, for example, DecimalFormat instead to parse the string.
DecimalFormat format = new DecimalFormat("##.#");
Double value = (Double) format.parse("12,3");
how to limit the number of digits of the whole number itself not just the fractional part of the number after the decimal?
for example using the number 54321.12574857f with the method DecimalFormat("##0.##"); it should return 543.12 or 321.12, the two decimal places after the decimal are correct however I want to reduce the numbers before the decimal to only 3. how is this done?
after entering the number 54321.12574857 I was using the parameter for the DecimalFormat method of ##0.## and I was expecting 543.12, or 321.12 not what I am getting which is 54321.12
the only numbers changed by DecimalFormat method are those after the decimal
float inputNumber = 54321.12574857f;
DecimalFormat decimalFormat = new DecimalFormat("##0.##");
String newNumber = String.valueOf(decimalFormat.format(inputNumber));
System.out.println(newNumber);
Try some thing like this,
DecimalFormat decimalFormat = new DecimalFormat("##0.##");
String newNumber = String.valueOf(decimalFormat.format(inputNumber/100));
I want to convert a number to a 2 decimal places (Always show two decimal places) in runtime. I tried some code but it only does, as shown below
20.03034 >> 20.03
20.3 >> 20.3 ( my code only rounds not converts )
however, I want it to do this:
20.03034 >> 20.03
20.3 >> 20.30 (convert it to two decimal places)
My code below:
angle = a variable
angle_screen = a variable
DecimalFormat df = new DecimalFormat("#.##");
angle = Double.valueOf(df.format(angle));
angle_screen.setText(String.valueOf(angle) + tmp);
Any help on how to do this would be great, thanks.
try this new DecimalFormat("#.00");
update:
double angle = 20.3034;
DecimalFormat df = new DecimalFormat("#.00");
String angleFormated = df.format(angle);
System.out.println(angleFormated); //output 20.30
Your code wasn't using the decimalformat correctly
The 0 in the pattern means an obligatory digit, the # means optional digit.
update 2: check bellow answer
If you want 0.2677 formatted as 0.27 you should use new DecimalFormat("0.00"); otherwise it will be .27
DecimalFormat df=new DecimalFormat("0.00");
Use this code to get exact two decimal points.
Even if the value is 0.0 it will give u 0.00 as output.
Instead if you use:
DecimalFormat df=new DecimalFormat("#.00");
It wont convert 0.2659 into 0.27. You will get an answer like .27.
Try this: String.format("%.2f", angle);
Try
DecimalFormat df = new DecimalFormat("#,##0.00");