2

I generated Excel with Data from BD, All is correct a except the type float.

In my BD I have "3.2" but in my Excell I have -> 3,20000004768372

    try {

        XSSFSheet sheet = workbook.createSheet(eyelash);
        XSSFDataFormat format = workbook.createDataFormat();
        style.setDataFormat(format.getFormat("Text")); //I need type text for others inputs in my Excel
        styleRow.setDataFormat(format.getFormat("Text"));
        String[] header = {"width"};
        XSSFRow rowhead = sheet.createRow((short) 0);
        XSSFCell cell;

            int myRowData = 1;
            XSSFRow row = sheet.createRow((short) myRowData);

            List<MyClass> list = this.select();

            for (int i = 0; i < list.size(); i++) {

                row.createCell(0).setCellValue(list.get(i).getWidth()); 
                myRowData++;
                row = sheet.createRow((short) myRowData);

        }

    } catch (Exception ex) {}

CLASS JAVA->

public class MyClass{

 private float width;

 public MyClass(){}

 public MyClass(float width){
   width=width;
 }

public void setWidth(float width) {
    this.width= width;
}


public Float getWidth() {
    return width;
} 

}

I can't use round because else never know width real.

EduBw
  • 866
  • 5
  • 20
  • 40

1 Answers1

1

If the precision of the values is a problem you should look at the difference between floats and doubles, as seen here and here

  • No,no In my BD I only insert "Float" 3,2 for example, but when I build excel I get 3,2000000000000004626267272 – EduBw Aug 03 '18 at 11:32
  • You can specify 3.2 but the floating point math will still add extra 'digits' to the end. Try it with doubles or a more precise data type, instead. –  Aug 03 '18 at 11:34
  • How do I do it so that it does not happen? – EduBw Aug 03 '18 at 11:36
  • You can have a look at [these](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html) data types, generally double would be better for the purpose of your requirements but... if you were to be using this for financial purposes using ints/bigints/longs etc, and cents vs decimals and dollars is generally a better approach. –  Aug 03 '18 at 11:40
  • Thanks I used Double. =) – EduBw Aug 03 '18 at 11:49
  • @EduBw If that fixed your issue could you accept/vote this answer to close the question? –  Aug 03 '18 at 12:13