Inline editing 4.2 appearing as 4.1999998
Inline editing 4.2 appearing as 4.1999998
Using datatable and Editor (in line) to allow a user to enter sample results.
Numbers such as 4.2 are being stores correctly in MY MSSQL DB but being displayed with a floating point error (I assume). Anywas I've got found the initial display by using ColumnDefs on the Datatable:
columnDefs: [
{
render: $.fn.dataTable.render.number(',', '.', 1),
targets: [3,4,5,6,7,8]
}
],
which is great, however when I click in the 4.2 Aspergillus in the 2nd row I get:
Is there something similar to the columnDef for the datatable I can use? or is there another solution to get it to be 4.2 instead?
Thank you inadvance for any help given
Answers
Can you post the JSON that's returned from the server for that record, please. I'd be interested to see if that has
4.2
or the incorrect number.Colin
Hi Colin... your right...The JSON is presenting the wrong value:
MSSQL is showing the correct values:
just need the editor to round it up to 1 dp
On the client-side Editor doesn't have a formatter for modifying values as they go into a field for editing. It always operates on the raw value.
So the solution here would be to understand why you are getting 4.19999... in the JSON data in the first place. I would have assumed it was being stored as an IEEE754 number with its limited precision (which is what Javascript uses), but it is shown as a string above, so I'm not certain that is the issue.
Perhaps you can clarify where the data is coming from and if it is 4.19999... in the data store, whatever that is.
Allan
basically I use PHP, this is what I use to retrieve the data from MSSQL and convert to JSON.
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Options,
DataTables\Editor\Upload,
DataTables\Editor\Validate;
$reqID = 0;
if (isset($_GET['projectID'])) {
$reqID = $_GET['projectID'];
// ToCleaned($reqID);
}
// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'tblAQIlocationSamples' )
->field(
Field::inst( 'tblAQIlocationSamples.id' ),
Field::inst( 'tblAQIlocsAndMeasures.locationNo' ),
Field::inst( 'tblAQIlocsAndMeasures.locationName' ),
Field::inst( 'tblAQIlocationSamples.sampleNo' ),
Field::inst( 'tblAQIlocationSamples.bacteriaCFU' ),
Field::inst( 'tblAQIlocationSamples.aspergillusCFU' ),
Field::inst( 'tblAQIlocationSamples.totalMouldCFU' ),
Field::inst( 'tblAQIlocationSamples.yeastCFU' ),
Field::inst( 'tblAQIlocationSamples.penicilliumCFU' )
)
->where('tblAQIlocationSamples.projectID',$reqID)
->where('tblAQIlocationSamples.sampleType','Airstrip')
->leftjoin( 'tblAQIlocsAndMeasures', 'tblAQIlocsAndMeasures.id', '=', 'tblAQIlocationSamples.AQIlocationID' )
->process( $_POST )
->json();
and this is the JSON response:
{"data":[{"DT_RowId":"row_14","tblAQIlocationSamples":{"id":"14","sampleNo":"1","bacteriaCFU":"5.0","aspergillusCFU":"4.0","totalMouldCFU":"26.0","yeastCFU":"5.0","penicilliumCFU":"3.0"},"tblAQIlocsAndMeasures":{"locationNo":"01","locationName":"Outside"}},{"DT_RowId":"row_17","tblAQIlocationSamples":{"id":"17","sampleNo":"4","bacteriaCFU":"1.0","aspergillusCFU":"6.0","totalMouldCFU":"2.0","yeastCFU":"5.0","penicilliumCFU":"3.0"},"tblAQIlocsAndMeasures":{"locationNo":"02","locationName":"Dining Room no 2"}}],"options":[],"files":[]}
Thanks. In the JSON response there is
"aspergillusCFU":"4.0"
and"aspergillusCFU":"6.0"
which I presume are okay?But from your comment before when it is 4.2 there is an issue - and the JSON return is actually 4.19999...? Which is odd since those numbers are actually strings - there shouldn't be any rounding.
Thanks,
Allan
Hi ShaneBrennan,
I have the same problem, did you find a solution? If I look at the data via odbc, I do get the normal data.
Thanks
Max
@MadMax76 it would be worth following the same diagnostic steps as Allan suggested, and letting us know the results.
Colin
Hi Colin,
server is ms-sql 2012, there the numbers are shown:
in datatables:
I have to work on the sorting too....
field definition:
in the editor-definition I tried to work with a render, which did not change anything:
json-response:
I also tried to access the data via ms-access and odbc, there the numbers come correct.
I hope that info helps.
Max
Thanks for that, that is helpful. Do the numbers appear long in the Ajax as well? Or are they 2dp there too?
Colin
sorry but what do you mean with "in the ajax"?
The data as it's sent from the server - you'll see it in the browser's network tab in the developer tools.
Colin
If that is the same as the Json-Response: yes, there the numbers are "wrong" already
Two more bits of information that might help track this down please:
->debug(true)
immediately before the->process(...)
can please? Then show me the JSON response from the server when you create a new row.It's almost certainly an IEEE 754 number issue, we just need to track down where the number modification is happening. It isn't
fromDecimalChar
as that is just doing a string replace, so if there is any other code you have which is doing number calculation on the fields, could you let us see that too please?Thanks,
Allan
I updated numbers to 35,11 and 42,13, here is the json
if this does not help i set up a test page for you
Max
Hi Max,
That confirms that the issue is not coming from the SQL database - the rounding error is happening somewhere before that. I'm not yet clear if it is on the Javascript or PHP side through. Can you show me the parameters being submitted to the server please? Or, yes, if you can setup a test page, that would be ideal.
Allan
here you go:
http://freigabe.kontura.at/test/allan.html
NEW does not work - no idea why - but update does produce the error.
Max
Hi Max,
I'm getting a lot of 404 errors when loading that page and it doesn't do very much I'm afraid - for example, DataTables isn't loading:
Allan
sorry, due to another building lot I had an update of my datatable-download.
Not it works, although it isnt really beuatiful...
Thanks
max
Sorry! I'm still getting an error:
$.fn.dataTable is undefined
on the page.Your:
needs to be loaded after DataTables core (since it is trying to attach a function to the DataTables core objects).
Allan
I got all eroors away... Problem persists...
Thanks! That confirms that it isn't a client-side issue:
Can you show me an unabbreviated copy of
V_Rechnungsheader_test.php
please?Allan
here you go:
Excellent - thank you! Reading over that, trying to reproduce it here and also working with your example page it doesn't look like it is in the PHP either - this is what is being written to the database:
So my theory flips to the data type being used in the database. Are you using a
float
ordouble
? What is the schema for the database? If it isn't a decimal you are using, then that is what I would recommend changing the data type to. It is slower for calculations, but it also means that your numbers won't stored using an approximate value.The MySQL page on floats contains useful information on this.
Sorry this one is taking me a little while to fully diagnose. Hopefully we are nearly there!
Allan
The software used doesn't offer decimal as option, but I changed it on the server directly and this did the trick!
Thanks!
Max
Phew - we got there in the end .
Allan