Pregunta

I am using this code:

DecimalFormat df = new DecimalFormat();
df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(2);
float a=(float) 15000.345;
Sytem.out.println(df.format(a));

I am getting this output:15,000.35 I don't want comma to be come in this output. My output should be:15000.35.

What is best way for getting this output in Java?

¿Fue útil?

Solución 2

try

             DecimalFormat df = new DecimalFormat();
             df.setMinimumFractionDigits(2);
             df.setMaximumFractionDigits(2);
             df.setGroupingUsed(false);
             float a=(float) 15000.345;
             System.out.println(df.format(a));

and

 Sytem.out.println(df.format(a)); //wrong  //sytem

 System.out.println(df.format(a));//correct //System

Otros consejos

Read javadoc and use this:

df.setGroupingUsed(false);

Grouping size should be set. The default value is 3. See the Doc.

df.setGroupingSize(0);

Or you use setGroupingUsed.

  df.setGroupingUsed(false);

Your Full code

DecimalFormat df = new DecimalFormat();
df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(2);
df.setGroupingUsed(false);
float a=(float) 15000.345;
Sytem.out.println(df.format(a));

You can also pass #####.## as pattern

DecimalFormat df = new DecimalFormat("#####.##");

You can do it like this:

DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(currentLocale);
otherSymbols.setDecimalSeparator(',');
otherSymbols.setGroupingSeparator('.'); 
DecimalFormat df = new DecimalFormat(formatString, otherSymbols);

After that as you have done:

df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(2);
float a=(float) 15000.345;
System.out.println(df.format(a));

This will give you the intended result.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top