Can't write float/double values to DBF-file; reading is fine
dBase for Java
Brought to you by:
usacoder
When reading field with a float ore double value (e.g. 987.123) using a NumField everthing is fine. :-)
But when I try to write a float or double value like this, then only the valu before the comma is writte in the datebase-file :
...
myNumField.put (987.123f);
System.out.println ( "value in DBF-file = " + myNumField.get() ); --> value in DBF-file = 987
...
Actual workaround is to change the field-format from "N=NumField" to "C=CharField"; but from point of view this cannot be the correct way.
The junit test (see below) does get an error. The error does not truncate the field but is a floating point translation error.
Output from Junit...
testFloat(org.xBaseJ.test.TestFields)
junit.framework.ComparisonFailure: expected:<[987.123]> but was:<[ 987.122]>
I'll work on the floating point translation bug.
Can you supply more information about the truncation bug.
public void testFloat() {
try {
DBF db = new DBF("testfiles/float.dbf", true);
FloatField f = new FloatField("F", 10,3);
db.addField(f);
f.put(987.123f);
db.write();
db.close();
db = new DBF("testfiles/float.dbf");
f = (FloatField) db.getField("F");
db.read();
assertEquals("987.123", f.get());
} catch (xBaseJException e) {
fail(e.getMessage());
} catch (IOException e) {
fail(e.getMessage());
}
finally {
File f = new File("testfiles/float.dbf");
f.delete();
}
}
Hello,
from my point of view there're two different problems:
1.) Your "ComparisonFailiure" seen in your example.
2.) My problem in writing float-values. You can try this when using this short example:
public static void main(String[] args)
{
try
{
float floatValue = 987.123f;
DBF db = new DBF("TEST_DB.dbf", true);
NumField numField = new NumField("N", 7,3);
FloatField floatField = new FloatField("F", 7,3);
db.addField(numField);
db.addField(floatField);
System.out.println("intial value before write = "+floatValue);
numField.put(floatValue);
floatField.put(floatValue);
System.out.println("database-value (NumField) after write = "+numField.get());
System.out.println("database-value (FloatField) after write = "+floatField.get());
db.write();
db.close();
}
catch(Exception e)
{
e.printStackTrace();
}
The output:
intial value before write = 987.123
database-value (NumField) after write = 987.000
database-value (FloatField) after write = 987.000
Hello,
I've created another version of NumField; the attached class "NumField2.java". From my point of view this class could be a "fast-hack-solution" :-)
NumField2.java - a "fast-hack-solution" :-)
fixed in next release - 20140320
Last edit: Joe McVerry 2014-03-08