double - Java - Decimal Format Precision lost when returning a Float or Decimal -


i have reviewed number of threads related formatting numbers. part works except value 3.101. float , double have been used along ## , 00 format.

import java.text.decimalformat;  public class decimalformattest { public static void main(string[] args)  {     float d1 = -3.1011f;     double ds = -3.1011;     decimalformat df = new decimalformat("#.##");     system.out.println (double.valueof(df.format(d1)));     system.out.println (double.valueof(df.format(ds)));      decimalformat df2 = new decimalformat("#.00");     system.out.println (double.valueof(df2.format(d1)));     system.out.println (double.valueof(df2.format(ds)));  } } 

the output :

-3.1 -3.1 -3.1 -3.1 

expected output :

-3.10 -3.10 -3.10 -3.10 

as mentioned above, works other numbers have tested. reason causing issue.

any ideas makes number different , step needed 2nd digit?

keep in mind, key want return float or double.

try this.

public static void main(string[] args)      {         float d1 = -3.1011f;         double ds = -3.1011;         decimalformat df = new decimalformat("0.00");         system.out.println (df.format(d1));         system.out.println (df.format(ds));          decimalformat df2 = new decimalformat("0.00");         system.out.println (df2.format(d1));         system.out.println (df2.format(ds));      } 

Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -