tags:

views:

139

answers:

1

Hello, I have two fields - amount (decimal (11, 2)) - gift_amount (decimal (11, 2))

When I do an update on either for a value equal to or below 999.99, it saves correctly.

However, if I go over that, then it drops the value right back to down 1 - 10.

Is this a known issue or am I going wrong using decimal?

Heres some PHP code of what I'm doing just to make it clearer (although I'm 100% its not the PHP's fault.

    if ($total_balance >= $cost) {

        if ($this->user->balance->gift_amount > 0) {
            $total_to_be_paid                   = number_format($cost, 2) - number_format($this->user->balance->gift_amount, 2);//figure out how much is left after the gift total
            $this->user->balance->gift_amount   -= number_format($cost, 2); //deduct from the gift balance
            $this->user->balance->gift_amount   = (number_format($this->user->balance->gift_amount, 2) < 0) ? number_format(00.00, 2) : number_format($this->user->balance->gift_amount, 2); //if the gift balance went below 0, lets set it to 0

            if ($total_to_be_paid > 0) {
                $this->user->balance->amount = number_format($this->user->balance->amount, 2) - number_format($total_to_be_paid, 2);
            }

        } else {
            $this->user->balance->amount = number_format($this->user->balance->amount, 2) - number_format($cost, 2);
        }

        if ($object = Model_ClipBought::create(array('clip_id' => $clip->id, 'user_id' => $this->user->id, 'currency_name' => $user_currency, 'cost' => $cost, 'downloads' => $clip->downloads, 'expires' => time() + ($clip->expires * 86400)))) {
            $this->user->balance->save();
            $download = new Model_Download(ROOT_PATH."/public/files/Clip/$clip->file_url");
            $download->execute();
        } else {
            throw new exception('We could not finish the purchase, this has been reported, sorry for the inconvenience.');
        }
    } else {
        throw new exception('You dont have enough money in your account todo this');
    }

    exit;
}
+1  A: 

You should not be using number_format until it's time to actually output/display the number to the user. It inserts your system's default thousand seperator (a comma by default):

number_format(999.99, 2) -> 999.99
number_format(1234.56, 2) -> 1,234.56

If you use these values in subsequent calculations in PHP, or try to insert verbatim into MySQL, you'll get funky values:

 2345.67 + 1.0 = 2346.67

but using number_format() gives you this parsing sequence:

 number_format(2345.67) + 1.0 -> "2,345.67" + 1.0
 "2,345.67" + 1.0 ->   "2" + 1.0
 2 + 1.0 -> 3

Notice how "2,345.67" was truncated down to just "2" - the comma turns your nice number into a string, and now you're bound by string->integer parsing rules, which drops everything after the first non-numeric character in the string.

If you're trying to keep everything down to 2 decimal places throughout the calculation sequence, consider using sprintf('%0.2f', $value) instead.

Marc B