Weight Convert issue (old 0.x bug)
Posted: Tue Aug 04, 2009 4:43 am
This is another case where the function does more than it should.
There are 2 functions in the weight library class.
one is for converting
one is for formatting
The conversion one should expect a numeric value input and return a numeric value.
The format function should expect a numeric value input and return a string value.
But the convert function adds number_formatting with the thousand_point, which in essence returns a string.
So if you want to convert a 1000 kg product into lbs, the convert will take "1000" and return "2,204".
The comma is then assumed to be a string so when the $cart->getWeight() function tries to += that returned value, it will only use the first "2" and truncate the rest. So 1000kg becomes 2lbs which is very wrong.
The convert function should only handle the numeric conversion. This also simplifies the function:
This won't break any calls to this function as there was only a call from the cart library and the cart model, both from the getWeight function and both only supplying the value, from, and to
There are 2 functions in the weight library class.
one is for converting
one is for formatting
The conversion one should expect a numeric value input and return a numeric value.
The format function should expect a numeric value input and return a string value.
But the convert function adds number_formatting with the thousand_point, which in essence returns a string.
So if you want to convert a 1000 kg product into lbs, the convert will take "1000" and return "2,204".
The comma is then assumed to be a string so when the $cart->getWeight() function tries to += that returned value, it will only use the first "2" and truncate the rest. So 1000kg becomes 2lbs which is very wrong.
The convert function should only handle the numeric conversion. This also simplifies the function:
Code: Select all
public function convert($value, $from, $to) {
if ($from != $to) { $value=($value * (float)$this->rules[$from][$to]); }
return $value;
}