Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extension of ceil() function. I suck at maths :( 2

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
FR

Hi,

I'm wondering how I could make a function that do ceil() on non-decimals like this :

123 becomes 130
12 345 becomes 12 400
146 904 becomes 147 000

Any idea?

Thanks! :)
 
Hi

See jpadie's generic formula in thread434-1586020, replace [tt]round()[/tt] with [tt]ceil()[/tt] and play with the $roundto value. ( I did not understood your rounding criteria so far. )

Feherke.
 
if these are string values with the space in place then you need to get rid of the spaces before treating them as numbers
Code:
str_replace(' ', '', $number);
 
Hi again :)

Okay, here is what I came up with, with your support :

Code:
function hd_ceil_weird($num) {

$num = str_replace(" ", "", $num);

$of = 3;

    if (strlen($num) <= $of) $of--;

$roundto = ("1" . str_repeat("0", (strlen($num) - $of))) * 1;
$num = number_format((ceil ($num/$roundto) * $roundto), 0, '.', ' ');;

return $num;

}

echo hd_ceil_weird (123) . "<br />";
echo hd_ceil_weird ("12 345") . "<br />";
echo hd_ceil_weird (146904) . "<br />";

Thanks again! :)
 
I'm interested. Why are you using those rounding rules?

i thought I had understood the rules from your examples but on closer examination I cannot derive a pattern from them

sleidia said:
123 becomes 130
12 345 becomes 12 400
146 904 becomes 147 000

1. two significant figures and rounding to tens
2. three significant figures and rounding to hundreds
3. three significant figures (rounding to hundreds or thousands)


 
Interested? ahaha

Actually, I had to change to
$of = 3;
in order to obtain exactly the result I needed.

I'm using this in order to display my prices on my website.
Since the prices are automatically converted into other currencies depending on the country/subdomain, I thought it would be nicer to make them easier to remember and, at the same time, add a margin so that whenever the exchange rates change, the prices displayed remain the same.

I'm not sure I was clear enough ahaha ;)

 
Ooops ... I meant :
I had to change to
$of = 2;
 
that's interesting.
I wrote a class once (last year) to grab about 14 different currency pairs and display localised prices. i added a margin function (slightly in excess of my bank commission) and a rounding function to make the prices look neat too. but the rounding function was only applied after the conversion and margin.

 
Maybe it's because you didn't need money as much as I do! ahaha ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top