I have a number that I need to limit / truncate down to two decimal places - so I need 6.999 to become 6.99. However, all of the available sprintf/number_format functions available will round everything up to 7, which I can't have.
Ideas?
I have a number that I need to limit / truncate down to two decimal places - so I need 6.999 to become 6.99. However, all of the available sprintf/number_format functions available will round everything up to 7, which I can't have.
Ideas?
substr
Well if they're numbers, then your choices include [man]floor[/man], [man]ceil[/man], and [man]round[/man]. If you want to you can even go wild and multiply the number by a hundred to shift the decimal point a couple of places to the right, then divide by a hundred afterwards to shift it back again. (How long did you say you've been a member here?)
None of floor/ceil/round are options for me because I don't want to destroy the value I have - I can't have ANY rounding occur.
Here is an example:
Initial value is 69.99
I need to calculate 10% of that, which is 6.999.
I'm dealing strictly with currency, so I need to loose that third 9. However, if I round up that turns the number to 7, which is a whole cent higher than 6.99.
These are being used as discounts, so 69.99 - 6.999 turns out to be 62.991. However, I want 69.99 - 6.99, which turns out to be 63. Because of this problems with rounding the decimal place, the calculations are all off by a single cent.
Why not just perform the truncation after you have performed all the arithmetic that you need to perform?
Because I'd be formatting the wrong value.
Original value = 6.999
If I use sprintf/number_format/round etc to force two decimal places, it ends up rounding the entire number to 7.00.
Item cost = 69.99
69.99 - 6.999 = 62.991
69.99 - 7 = 62.99
Now, if I use the number formatting options at this stage, I'd get either 62.99 or 63. Now, 63 is the correct value, but 7.00 is the incorrect value for the discount as it's one cent higher than it should be.
I need the discount to become 6.99, so that 69.99 - 6.99 = 63.
Assuming you have bcmath installed, could you not use something like bcdiv()?
Try the following snippet to find out:
$num = 69.99;
$newNum = bcdiv($num, '10', 2); // third paramter displays specified number of digits after the decimal
bcdiv doesn't round off anything.. just uses division and truncates x amount of digits after the decimal point.
Well, Weedpacket did suggest several alternatives...
<?php
function truncate($value, $precision) {
$multiplier = pow(10, $precision);
$value = (int)($value * $multiplier);
return $value / $multiplier;
}
$original_value = 6.999;
$item_cost = 69.99;
$original_value = truncate($original_value, 2);
$diff = $item_cost - $original_value;
echo $diff;
I really wanted to avoid having to multiply it and then divide again, but it seems to be the only way aside from some substr lameness. Multiplying it by 100 moved the decimal to the right by two places. Then, by casting it as an integer we loose the remaining decimals altogether. Dividing it returns the decimal to the same place.
I fail to see what's lame about substr. It does what it is meant to do, and does it well. You now have a few options presented within this thread that do not include substr.