Rounding issuse

Rounding issuse

bbrindzabbrindza Posts: 316Questions: 73Answers: 1

I am having a problem with rounding in DataTable Editor and I need some insight on why this calculation is not returning the correct result. I suspect it is happening in the front-end JS. I may need a rounding function here.

Here is the PHP server side results and script.

Before: HRWPER: 3.5; HRYREG: 13.01
After : HRWAMT: 0.45535; HRWNEW 13.46535

file_put_contents(
   $temp_file,
   "Before: HRWPER: {$values['HRWPER']}; HRYREG: {$values['HRYREG']}\n"
);

// Calculate Amount of Increase based on Percent
$HRWAMT = $values['HRWPER'] * .01 * $values['HRYREG'];

// Calculate New Hourly Rate
$HRWNEW = $values['HRYREG'] + $HRWAMT;

file_put_contents(
   $temp_file,
   "After : HRWAMT: $HRWAMT; HRWNEW $HRWNEW\n",
   FILE_APPEND
);

Here is the JS and results.

            { data: "HRWPER", render: $.fn.dataTable.render.number( ',', '.', 1, '', '%' ) }, 
            { data: "HRYREG", render: $.fn.dataTable.render.number( ',', '.', 2, '$' ) },   
            { data: "HRWNEW", render: $.fn.dataTable.render.number( ',', '.', 2, '$' ) }, 

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @bbrindza ,

    It's hard to understand the issue without more explanation. Are you saying that "0.45535" is being displayed as "0.45", and not "0.46"?

    I just tried it here - with three columns, 1. original number, 2, using the built-in render function, and 3. a custom renderer.

    In my examples, 0.45535 is always being displayed as 0.46 as expected. If this is your problem, it would be worth logging what the values are when they hit the table, as it might getting mangled by the JSON packaging either on the server, or on the client.

    Cheers,

    Colin

  • bbrindzabbrindza Posts: 316Questions: 73Answers: 1

    Colin,
    You are correct. It writes the value of 0.45 to a 2 decimal position DB2 field named HRWAMT which should be 0.47. It also write the value of 13.46 to a 2 decimal position DB2 field named HRWNEW which should be 13.47. Perhaps the rounding issuse are in in the PHP and not the JS. Thoughts?

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @bbrindza ,

    It's definitely not in the JS, given I'm using the same numbers without an issue, so it would be worth adding as much debug as you can at each of the stress points - before leaving the server, when picked up by the client, before/after JSONification, etc.

    Cheers,

    Colin

  • allanallan Posts: 63,508Questions: 1Answers: 10,471 Site admin

    Let's take this one:

    Before: HRWPER: 3.5; HRYREG: 13.01
    $HRWAMT = $values['HRWPER'] * .01 * $values['HRYREG'];

    By my calculator that should be 0.45535. I'm not sure where you get 0.47 for it?

    It sounds like DB2 is truncating the value, so its effectively taking the floor, but even rounded it would be 0.46.

    Allan

  • bbrindzabbrindza Posts: 316Questions: 73Answers: 1

    Sorry Allan it should be 0.46. Fat fingers typing number on an iPad. So if I round ceiling before it is written into the DB2 field , it should round it up to 0.46.

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Ha, yeah, I did say to Allan you have made a typo... Did you get anywhere with this? If you suspect it's a DB issue, you could just send the full numbers across and do the sums on the client.

This discussion has been closed.