This topic is locked
[SOLVED]

 Mysql Calculation and View as Currency PHPrunner

2/24/2020 1:56:24 PM
PHPRunner General questions
lefty author

I have two fields in a table and an alias.

I have a MySQL statement setup for example
(case_cost/packed_qty) As bottle_cost,
Actual Calculation: 138.92 / 12 = 11.57 // Which is correct
Case Cost - Decimal - 11,2

Packed Quantity - INT
In PHP list page I have bottle_cost setup as read-only and view as Currency . The result on the list page shows 11.58 . It is off by one cent. I see it rounds different than PHP rounds. There are other calculations that continue and as the calculations continue , my resulting numbers are off by alot .
I have tried custom view $value = "$" . $data["case_cost"] / $data["packed_qty"] and I get the same 11.58 ( Just testing diff between custom view and view as currency - Same Result ) . My sql query in PHPrunner / query result tab returns 11.583333 when testing which explains it but it is wrong. Does not make sense to me! Always thought that Decimal 11,2 is more accurate than using Double or Float but now I am not sure?
11.57 should be the correct value , but I always get 11.58 in php view as currency . This is just one example , some records get lucky and are correct - I believe because of the way rounding is used, while others have this problem. Any ideas would be appreciated.

W
WilliamBDevClub member 2/24/2020

138.92 / 12 = 11.57666666666667
Shouldn't it be 11.58?? That's how I would always calculate it.
Using Decimal 11,2 will give you 11.58 as it rounds.

Admin 2/24/2020

@BillyB,
is spot on, the result of this division is 11.57(6).
If you want it to be consistent you need to round it up or down right in the SQL query. Check round() function:

https://www.mysqltutorial.org/mysql-math-functions/mysql-round/

lefty author 2/24/2020



@BillyB,
is spot on, the result of this division is 11.57(6).
If you want it to be consistent you need to round it up or down right in the SQL query. Check round() function:

https://www.mysqltut...ns/mysql-round/


This all makes sense but if it indeed should round to 11.58 , it would make the case cost $138.96 which now becomes the wrong case cost instead of $138.92. Which in turn throws off all the calculations of other fields.
I did not show the whole query but here is another part of it
SELECT

flditemID,

branddesc,

size,

fldcommodity,

division_name,

minbottle,

case_cost,

bottle_post,

case_post,

packed_qty,

(case_cost/packed_qty) AS bottle_cost,

((minbottle - (case_cost / packed_qty)) / minbottle) 100 AS gross_profit,

IF(size = '750ML', ((case_cost/packed_qty)/25.36), 0) AS 750OZ,

IF(size = '750ML', ((case_cost/packed_qty)/25.36
6),0) AS 750Glass,

IF(size = '750ML', Round(((case_cost/packed_qty)/25.366)4), 0) AS 750Sell, / This is suggested sell price by trade standards/

IF(size = '750ML', (((case_cost/packed_qty)/25.366)4), 0)- IF(size = '750ML',((case_cost/packed_qty)/25.366),0) AS 750profit,
IF(size = '1.5L', ((case_cost/packed_qty)/50.72), 0) AS 15OZ,

IF(size = '1.5L', ((case_cost/packed_qty)/50.72
6), 0) AS 15Glass,

IF(size = '1.5L', Round(((case_cost/packed_qty)/50.726)4), 0) AS 15Sell, / This is suggested sell price by trade standards /

IF(size = '1.5L', (((case_cost/packed_qty)/50.726)4), 0)- (IF(size = '1.5L',((case_cost/packed_qty)/50.726), 0)) AS 15profit,
IF (size = '750ML' , ((case_cost/packed_qty)/25.36
6)/ (((case_cost/packed_qty)/25.366)4),0) 100 As Glassper,

IF (size = '1.5L' , ((case_cost/packed_qty)/50.72
6)/ (((case_cost/packed_qty)/50.726)4),0) * 100 As GlCstper,

UPC

From..............
This is premature as I just saw this reply and I must be misunderstanding something . Will have to investigate.and the other a third party mobile application result in $11.57.???





_I pull the import in a CSV file , from SAP and it runs Mysqlimport.exe from a batch file and ( Task Scheduler ) and imports into MySQL which in turn PHPrunner picks it up. But PHPrunner is running my query as I show above with the incorrect result.
_I need for the query to match what a ( MAJOR ERP SYSTEM SAP )Shows as 11.57 as I have to be consistent with the connected software . How can I achieve this? I am now thinking to moving all these calculations to before list record processed if that is better or even Javascript onload . Any help appreciated.

W
WilliamBDevClub member 2/24/2020



This all makes sense but if it indeed should round to 11.58 , it would make the case cost $138.96 which now becomes the wrong case cost instead of $138.92. Which in turn throws off all the calculations of other fields.

. Then again you have a point as if I multiply 11.57 12 = 138.84 is also wrong but so is 11.58 12 = 138.96. hmmm never ran into this before I guess??
I did not show the whole query but here is another part of it
SELECT

flditemID,

branddesc,

size,

fldcommodity,

division_name,

minbottle,

case_cost,

bottle_post,

case_post,

packed_qty,

(case_cost/packed_qty) AS bottle_cost,

((minbottle - (case_cost / packed_qty)) / minbottle) 100 AS gross_profit,

IF(size = '750ML', ((case_cost/packed_qty)/25.36), 0) AS 750OZ,

IF(size = '750ML', ((case_cost/packed_qty)/25.36
6),0) AS 750Glass,

IF(size = '750ML', Round(((case_cost/packed_qty)/25.366)4), 0) AS 750Sell, // This is suggested sell price by trade standards

IF(size = '750ML', (((case_cost/packed_qty)/25.366)4), 0)- IF(size = '750ML',((case_cost/packed_qty)/25.366),0) AS 750profit,
IF(size = '1.5L', ((case_cost/packed_qty)/50.72), 0) AS 15OZ,

IF(size = '1.5L', ((case_cost/packed_qty)/50.72
6), 0) AS 15Glass,

IF(size = '1.5L', Round(((case_cost/packed_qty)/50.726)4), 0) AS 15Sell,

IF(size = '1.5L', (((case_cost/packed_qty)/50.726)4), 0)- (IF(size = '1.5L',((case_cost/packed_qty)/50.726), 0)) AS 15profit,
IF (size = '750ML' , ((case_cost/packed_qty)/25.36
6)/ (((case_cost/packed_qty)/25.366)4),0) 100 As Glassper,

IF (size = '1.5L' , ((case_cost/packed_qty)/50.72
6)/ (((case_cost/packed_qty)/50.726)4),0) * 100 As GlCstper,
UPC

FROM supplier_data

WHERE (size = '750ML' OR size = '1.5L') AND (fldcommodity = 'Sparkling Wine/Champ' OR fldcommodity = 'Still Wine' OR fldcommodity = 'Fortified Wine')
This is premature as I just saw this reply and I must be misunderstanding something . Will have to investigate.and the other a third party mobile application result in $11.57.???





_I pull the import in a CSV file , from SAP and it runs Mysqlimport.exe from a batch file and ( Task Scheduler ) and imports into MySQL which in turn PHPrunner picks it up. But PHPrunner is running my query as I show above with the incorrect result.
_I need for the query to match what a ( MAJOR ERP SYSTEM SAP )Shows as 11.57. How can I achieve this?


I'm not sure but maybe you are looking for this. There are a few examples that will not round in this post. https://stackoverflow.com/questions/3833137/how-to-make-number-format-not-to-round-numbers-up

lefty author 2/24/2020



I'm not sure but maybe you are looking for this. There are a few examples that will not round in this post. https://stackoverflo...ound-numbers-up


Thanks ,

I looked at something similar earlier today , I think I will test

[color="#303336"]floor function

Admin 2/25/2020

I still recommend doing it in MySQL to avoid any potential inconsistencies. If you need to round it down use TRUNCATE() function:

https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_truncate

lefty author 2/25/2020



I still recommend doing it in MySQL to avoid any potential inconsistencies. If you need to round it down use TRUNCATE() function:

https://dev.mysql.co...nction_truncate


This did it.
Truncate((case_cost/packed_qty),2) AS bottle_cost,
Thank you Both for the help.