Jump to content
driven22

Problem with round weight

Recommended Posts

In admin, entering the product weight 0.51 after saving, I still have 0.00. When I type 1.2 after saving I have 1. Oscommerce 2.3.4.1 CE, php 7.4. I am asking for help, I have a problem with incorrect decimal rounding in several places.

 

// Wrapper function for round()
function tep_round($number, $precision) {
  if (strpos($number, '.') && (strlen(substr($number, strpos($number, '.') + 1)) > $precision)) {
    $number = substr($number, 0, strpos($number, '.') + 1 + $precision + 1);

    if (substr($number, -1) >= 5) {
      if ($precision > 1) {
        $number = substr($number, 0, -1) + ('0.' . str_repeat(0, $precision - 1) . '1');
      } elseif ($precision == 1) {
        $number = substr($number, 0, -1) + 0.1;
      } else {
        $number = substr($number, 0, -1) + 1;
      }
    } else {
      $number = substr($number, 0, -1);
    }
  }

  return $number;
}

 

Share this post


Link to post
Share on other sites

In db also the differences between what is visible in the store and what is in db.

Same in oreders_products. Final price rounded down :( .How to fix it?

 

problem1.png

problem2.png

Share this post


Link to post
Share on other sites

The function you showed is the standard one and works fine in other shops so that is not the problem. My guess is that your code has been altered somehow to cause this or maybe your database was changed for some reason. You will need to troubleshoot it to find out what is going on. I suggest that after this line in the admin function

On 2/19/2022 at 3:32 AM, driven22 said:

function tep_round($number, $precision) {

add the following:

echo 'Price '. $number . ' Precision: ' . $precision;
return '1.23';

Then change a price for a product. The above change will display what is sent to that function and then replaces it with a value.  Make sure the price you entered is showing with that new code and that the saved value is the new one. If the full price is not being sent to the function then something in the categories file has been changed to alter the price. If the price sent is correct but the new figure is not stored then the problem is in the saving to database code or the database.


Support Links:

For Hire: Contact me for anything you need help with for your shop: upgrading, hosting, repairs, code written, etc.

Need Help? See this thread and provide the information requested.

How to Upgrade to the latest version

Recommended SEO Addons

Share this post


Link to post
Share on other sites

I changed to admin / general to

function tep_round($number, $precision) {
  echo 'Price '. $number . ' Precision: ' . $precision;
return '1.23';
}

but nothing has changed. I think the "," and "." are misinterpreted" .In data transfer shop -> db. In admin / Currency I changed decimal point to "." and thousands point was "," but it also didn't help. I changed the languages / english setlocate to LC_TIME to LC ALL in the file but it also did not help.

I don't know where to look for the reason why this is happening. When I type the weight of 0.51 kg in the product after writing the rounds down to 0.00. The price of the product is remembered on the store's side, but already rounded down in the database. In admin / special, when I want to enter a discount from 25 to 24.50 round down to 24. When I want to make a percentage discount, it also calculates incorrectly. This came after switching from PHP 7.2 to 7.3 / 7.4. It looks like a simple mistake - but I don't know where to look.

Share this post


Link to post
Share on other sites

One more thing. in the admin, when changing the price of the product, if I enter 25.60 and save - when I go back to the edition of the product, I have a value of 25.60. If i enter 25,60 after writing and editing the product, the price is rounded to 25.00 (as it is seen in the database). It would be a problem with the decimal interpretation ( "," and ".").

Edited by driven22
correct text

Share this post


Link to post
Share on other sites

This has changed in the overview of the products after
 

function tep_round($number, $precision) {
  echo 'Price '. $number . ' Precision: ' . $precision;
return '1.23';
}



image.png.118132e34f75b5fad4d4b37bbf4ddb40.png

Share this post


Link to post
Share on other sites

@driven22 Both values are wrong. The price is what the code is being told to round. Assuming you entered a number with a fractional part, that is being removed before it gets to the round function. And the  precision should be a number, like 2, as I recall. Take a look in admin->Localization->Currencies and make sure the default currency is setup correctly. You may want to delete it and add it back in to make sure the database entries are not corrupted.  


Support Links:

For Hire: Contact me for anything you need help with for your shop: upgrading, hosting, repairs, code written, etc.

Need Help? See this thread and provide the information requested.

How to Upgrade to the latest version

Recommended SEO Addons

Share this post


Link to post
Share on other sites

I removed the currency and added it again. Unfortunately, nothing has changed. Order Total in the admin is ok, the database is rounded to full values :(

Share this post


Link to post
Share on other sites

On the other hand, it works without any problems - I enter order total in the database, for example, 25.56 and that's what I have in admin. The same with the weight - everything is OK. Unfortunately, when I enter order total, special, weight from the store's side towards the database, I have values rounded down to whole numbers.

Share this post


Link to post
Share on other sites
11 hours ago, driven22 said:

Ok, I solved this problem :)

You should post the solution for the benefit of others, or next time they may not be inclined to help you.

Share this post


Link to post
Share on other sites

For half a year I was looking for a solution, so let me give the solution after 24 hours of tests.. :)
Problem was as I had suspected in the interpretation "," and "." shop - db. Despite writing in lanugaes / english @setlocale (LC_TIME .... or LC_ALL did not work. But after typing in aplication_top.php (also in admin):
setlocale(LC_ALL, "pl_PL");
setlocale(LC_NUMERIC, 'C')
;
Weight works fine in categories, also price, order_total, special (by price and%), export csv displays original values. I have been looking for a solution for over half a year on the forum, trying in the store. There was info everywhere that setlocale only fits in languages. Something was probably blocking setloc but hence the problem (but I can see, not only for me).
A simple solution, and I spent so much time on it .. ahh

 

 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×