Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Currency exchange rates adds itw own numbers?


sbrenusa

Recommended Posts

Sorry for my ignorance, but I don't know where this is happening or where to make a change. I have a base currency of USD and two additional currencies - euro and dominican peso. When I change the dominican peso to 3.65 it automatically changes my exchange rate to 33.65000153, if I put it out with all zeros it still makes the same change. If I put it out with another number for example: 33.63914373 it changes this number to 33.63914490. WHY??

 

My dominican prices are set to go to no decimal places (USD goes to two) so I can set rates in dollars that look normal in pesos. $3.27 US to $110 pesos. Instead I am constantly getting the wrong prices in pesos. Any ideas?

 

Thanks in advance. stephanie

Link to comment
Share on other sites

Ok -- further info, I am able to put in 34.0000000 as an exchange rate but anything close to the 33.65 gets changed. If I chang ein admin that I want 2 decimal points showing I get the correct number RD110.16 if I choose 1 decimal place I get RD108.8 and if I choose zero decimal points I get RD102. I need it to say 110 but with no places since there is no currency lower than 1. Very strange, appreciate any support on this.

Link to comment
Share on other sites

Still trying to figure this out. Can anyone help me??

 

I think this is where the problem lies - rounding in the currencies table. I believe I copied this one from the catalog / includes / class / currencies.php (there are about 6 different currencies.php files.)

 

Does anyone see an error (I'm not a programmer but I can make basic changes as needed.)

 

Please take a look and let me know if you see any reason why setting the decimal places in the admin would be affecting the alternate currency product price display numbers so differently....

 

Thank you.

 

<?php

/*

$Id: currencies.php 1803 2008-01-11 18:16:37Z hpdl $

 

osCommerce, Open Source E-Commerce Solutions

http://www.oscommerce.com

 

Copyright © 2008 osCommerce

 

Released under the GNU General Public License

*/

 

////

// Class to handle currencies

// TABLES: currencies

class currencies {

var $currencies;

 

// class constructor

function currencies() {

$this->currencies = array();

$currencies_query = tep_db_query("select code, title, symbol_left, symbol_right, decimal_point, thousands_point, decimal_places, value from " . TABLE_CURRENCIES);

while ($currencies = tep_db_fetch_array($currencies_query)) {

$this->currencies[$currencies['code']] = array('title' => $currencies['title'],

'symbol_left' => $currencies['symbol_left'],

'symbol_right' => $currencies['symbol_right'],

'decimal_point' => $currencies['decimal_point'],

'thousands_point' => $currencies['thousands_point'],

'decimal_places' => $currencies['decimal_places'],

'value' => $currencies['value']);

}

}

 

// class methods

function format($number, $calculate_currency_value = true, $currency_type = '', $currency_value = '') {

global $currency;

 

if (empty($currency_type)) $currency_type = $currency;

 

if ($calculate_currency_value == true) {

$rate = (tep_not_null($currency_value)) ? $currency_value : $this->currencies[$currency_type]['value'];

$format_string = $this->currencies[$currency_type]['symbol_left'] . number_format(tep_round($number * $rate, $this->currencies[$currency_type]['decimal_places']), $this->currencies[$currency_type]['decimal_places'], $this->currencies[$currency_type]['decimal_point'], $this->currencies[$currency_type]['thousands_point']) . $this->currencies[$currency_type]['symbol_right'];

} else {

$format_string = $this->currencies[$currency_type]['symbol_left'] . number_format(tep_round($number, $this->currencies[$currency_type]['decimal_places']), $this->currencies[$currency_type]['decimal_places'], $this->currencies[$currency_type]['decimal_point'], $this->currencies[$currency_type]['thousands_point']) . $this->currencies[$currency_type]['symbol_right'];

}

 

return $format_string;

}

 

function calculate_price($products_price, $products_tax, $quantity = 1) {

global $currency;

 

return tep_round(tep_add_tax($products_price, $products_tax), $this->currencies[$currency]['decimal_places']) * $quantity;

}

 

function is_set($code) {

if (isset($this->currencies[$code]) && tep_not_null($this->currencies[$code])) {

return true;

} else {

return false;

}

}

 

function get_value($code) {

return $this->currencies[$code]['value'];

}

 

function get_decimal_places($code) {

return $this->currencies[$code]['decimal_places'];

}

 

function display_price($products_price, $products_tax, $quantity = 1) {

return $this->format($this->calculate_price($products_price, $products_tax, $quantity));

}

}

?>

Link to comment
Share on other sites

I think your problem is in the DB. Currencies 13 numbers rounded to 8 (from the currencies table)

 

value float (13,8)

 

Try working with those numbers and see if you can get it closer.

Link to comment
Share on other sites

I think your problem is in the DB. Currencies 13 numbers rounded to 8 (from the currencies table)

Try working with those numbers and see if you can get it closer.

 

Thank you. I was able to get the number of the exchange rate to stay stable by adjusting that field in the db, and either making the number a decimal or just changing the amount of places, but the price displayed is still off on the site. Any more ideas why with 0 decimals places chosen it would be 101 and with two its displayed as 110.16?

 

I'm using a template, so I'm going to try and compare the orginal files of the install and make sure nothing got changed in the process. I haven't added any contributions at this point so I don't think I've corrupted the file in any way. Very confusing.

 

It happens the same way on two different sites where I have this store set up, one I am using as a test, just to not screw anything up.

 

The decimal place in the db is saved as a character and not some kind of number which I thought was strange, but it does display correctly with 0 or 2 actual places, it's just the rounding up that is not correct.

 

Any help, please......

Link to comment
Share on other sites

So I'm still trying to just tweak the prices in dollars and exchange rate to see if I can get the round numbers in pesos. When I tried setting the price in dollars with a 35 exchange rate it shows 99.75 pesos with two decimal places and 105 with no decimal places - so it's rounding up in this case by 5 pesos? I just don't get it.....

Link to comment
Share on other sites

When I tried setting the price in dollars with a 35 exchange rate it shows 99.75 pesos with two decimal places and 105 with no decimal places - so it's rounding up in this case by 5 pesos? I just don't get it.....

I can tell you why you're getting the answers you're getting, but I won't be able to fix the problem.

 

With a 35 exchange rate, and a peso price of 99.75, that arrives at a peso to dollar rate of:

 

99.75/35 = 2.85 pesos per dollar.

 

So, what's happening when you set the decimal places to zero (no decimal places), it's rounding the 2.85 to 3.0

 

Now you have an exchange rate of 3.0 (pesos per dollar) times the price of $35:

 

3.0*35 = 105

 

That's why you get such a disparity in price.

 

It's just "sloppy math".

 

They're rounding, then multiplying. They should be multiplying FIRST, then rounding.

 

When working with floating point numbers, to get the most accurate answer, you must not round anything until you get to the final result (in this case, display price).

 

This is especially true with multiplication.

 

Anything less than that yields the results you are seeing.

 

There's just no "band-aid" for sloppy math.

:blush:

 

I've looked at the code (briefly), but don't have a good enough knowledge of it to fix it.

If I suggest you edit any file(s) make a backup first - I'm not perfect and neither are you.

 

"Given enough impetus a parallelogramatically shaped projectile can egress a circular orifice."

- Me -

 

"Headers already sent" - The definitive help

 

"Cannot redeclare ..." - How to find/fix it

 

SSL Implementation Help

 

Like this post? "Like" it again over there >

Link to comment
Share on other sites

Try this;

 

<?php
/*
$Id: currencies.php 1803 2008-01-11 18:16:37Z hpdl $

osCommerce, Open Source E-Commerce Solutions
http://www.oscommerce.com

Copyright © 2008 osCommerce

Released under the GNU General Public License
*/

////
// Class to handle currencies
// TABLES: currencies
class currencies {
var $currencies;

// class constructor
function currencies() {
$this->currencies = array();
$currencies_query = tep_db_query("select code, title, symbol_left, symbol_right, decimal_point, thousands_point, decimal_places, value from " . TABLE_CURRENCIES);
while ($currencies = tep_db_fetch_array($currencies_query)) {
$this->currencies[$currencies['code']] = array('title' => $currencies['title'],
'symbol_left' => $currencies['symbol_left'],
'symbol_right' => $currencies['symbol_right'],
'decimal_point' => $currencies['decimal_point'],
'thousands_point' => $currencies['thousands_point'],
'decimal_places' => $currencies['decimal_places'],
'value' => $currencies['value']);
}
}

// class methods
function format($number, $calculate_currency_value = true, $currency_type = '', $currency_value = '') {
global $currency;

if (empty($currency_type)) $currency_type = $currency;

if ($calculate_currency_value == true) {
$rate = (tep_not_null($currency_value)) ? $currency_value : $this->currencies[$currency_type]['value'];
$format_string = $this->currencies[$currency_type]['symbol_left'] . number_format(tep_round($number * $rate, $this->currencies[$currency_type]['decimal_places']), $this->currencies[$currency_type]['decimal_places'], $this->currencies[$currency_type]['decimal_point'], $this->currencies[$currency_type]['thousands_point']) . $this->currencies[$currency_type]['symbol_right'];
} else {
$format_string = $this->currencies[$currency_type]['symbol_left'] . number_format(tep_round($number, $this->currencies[$currency_type]['decimal_places']), $this->currencies[$currency_type]['decimal_places'], $this->currencies[$currency_type]['decimal_point'], $this->currencies[$currency_type]['thousands_point']) . $this->currencies[$currency_type]['symbol_right'];
}

return $format_string;
}
/*
function calculate_price($products_price, $products_tax, $quantity = 1) {
global $currency;

return tep_round(tep_add_tax($products_price, $products_tax), $this->currencies[$currency]['decimal_places']) * $quantity;
}
*/
function is_set($code) {
if (isset($this->currencies[$code]) && tep_not_null($this->currencies[$code])) {
return true;
} else {
return false;
}
}

function get_value($code) {
return $this->currencies[$code]['value'];
}

function get_decimal_places($code) {
return $this->currencies[$code]['decimal_places'];
}

function display_price($products_price, $products_tax, $quantity = 1) {
return $this->format($this->calculate_price($products_price, $products_tax, $quantity));
}
}
?>

 

I just commented out a function in the middle.

 

function calculate_price($products_price, $products_tax, $quantity = 1) {
global $currency;

return tep_round(tep_add_tax($products_price, $products_tax), $this->currencies[$currency]['decimal_places']) * $quantity;
}

Link to comment
Share on other sites

But that function is used later in the code:

 

function display_price($products_price, $products_tax, $quantity = 1) {
return $this->format($this->calculate_price($products_price, $products_tax, $quantity));

If you just comment it out, you'll get the infamous ".... function not found" error...

 

Correct?

:unsure:

 

Won't you need to replace it with something that actually works?

:unsure:

 

I'm not real familiar with what all the variable represent, but maybe this:

 

function calculate_price($products_price, $products_tax, $quantity = 1) {
global $currency;

return tep_round(tep_add_tax($products_price * $quantity, $products_tax), $this->currencies[$currency]['decimal_places']);
}

I think I multiplied the price time the quantity, then it rounds...

 

I'm not sure!

:lol:

If I suggest you edit any file(s) make a backup first - I'm not perfect and neither are you.

 

"Given enough impetus a parallelogramatically shaped projectile can egress a circular orifice."

- Me -

 

"Headers already sent" - The definitive help

 

"Cannot redeclare ..." - How to find/fix it

 

SSL Implementation Help

 

Like this post? "Like" it again over there >

Link to comment
Share on other sites

You might be correct, if that happens

 

 

return $this->format($this->calculate_price($products_price, $products_tax, $quantity));

 

 

to

 

return $this->format(tep_add_tax($products_price, $products_tax) * $quantity);

 

 

The round function was added in RC2, it is not in MS2.2 and it seems to be in the wrong place. I just downloaded RC2a and the file posted it accurate for the version. The option I gave was to take it out which converts it back to a 2.2 version of the page. I just don't know why it was added there.

Link to comment
Share on other sites

You might be correct, if that happens

return $this->format($this->calculate_price($products_price, $products_tax, $quantity));

to

 

return $this->format(tep_add_tax($products_price, $products_tax) * $quantity);

The round function was added in RC2, it is not in MS2.2 and it seems to be in the wrong place. I just downloaded RC2a and the file posted it accurate for the version. The option I gave was to take it out which converts it back to a 2.2 version of the page. I just don't know why it was added there.

 

 

Wow thanks to both you and Germ for the insights -- I never would have guessed that it rounded the price then multiplying the rate - guess it's not a worry for a lot of people using other currencies that still have "pennies" in their secondary currencies

 

I had just about given up hope and was trying out a contribution for "Friendly Pricing" which allows you set the price in either currency - so for the few things that I needed the round numbers for in pesos it was working, but it would then not have been good for the other items I need to peg to dollars. It also wasn't working with my template to pull the "Friendly Price" correctly, so I was basically going to suck it up and remake the template pages using STS - easier for me than the programming.

 

I'll give the code suggestions a try and let you know what works.

 

Thanks again. Stephanie

Link to comment
Share on other sites

return $this->format($this->calculate_price($products_price, $products_tax, $quantity));

to

return $this->format(tep_add_tax($products_price, $products_tax) * $quantity);

 

It worked like a charm. Thanks so much!!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...