Closed Thread Icon

Topic awaiting preservation: php calculation & notation question Pages that link to <a href="https://ozoneasylum.com/backlink?for=31479" title="Pages that link to Topic awaiting preservation: php calculation &amp;amp; notation question" rel="nofollow" >Topic awaiting preservation: php calculation &amp; notation question\

 
Author Thread
NetGhost
Neurotic (0) Inmate
Newly admitted

From:
Insane since: Nov 2009

posted posted 11-10-2009 18:05

Dear fellow inmates

Please help me with a task that kept me from doing real productive work all afternoon:

I am converting from square metres (Switzerland) to square feet (UK) like so:

code:
<?php $sqm = '230'; echo "$sqm m&sup2; / "; print round($sqm * 10.764); echo " sq ft";?>



Basically the code is working fine but once I use larger square metre figures, like so:

3000 m² / 32292 sq ft

...the notation is missing.

Now how do I get the square metres to be Swiss notation and the sq ft to be UK notation like so?

3'000 m² / 32,292 sq ft


Help will be much appreciated!

twItch^
Maniac (V) Mad Scientist

From: Denver, CO, USA
Insane since: Aug 2000

posted posted 11-10-2009 20:38
code:
<?php echo "$sqm m&sup2; / " . round($sqm * 10.764) . " sq ft"; ?>



Note what I did there--instead of having 3 print statements, I concatenated with the dot (".") operator. That should clear it up.

As for how to format the numbers returned, check out the function number_format() function, handles what you need.

-S

NetGhost
Obsessive-Compulsive (I) Inmate

From:
Insane since: Nov 2009

posted posted 11-10-2009 22:15

Thank you twItch^!

I figured it out:

code:
<?php $sqm = '6666'; echo number_format($sqm, 0, ',', '\'') . " m&sup2; / " . number_format(($sqm * 10.764), 0, ',', '\,') . " sq ft"; ?>



Output is:

6'666 m² / 71,753 sq ft

Perfect!

I do wonder however if there isn't a better way of doing it: I have to repeat the above code twice for every property (plot and living space).

This gives me about 10 instances of the same code on the same page.

There must be a better way...

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 11-11-2009 08:19

of course there is,
you need a php->Function

NetGhost
Obsessive-Compulsive (I) Inmate

From:
Insane since: Nov 2009

posted posted 11-19-2009 12:46

Thanks Tyberius!

Now I do understand the basics of php functions like so:

code:
<?php
function calcsqft($sqm){
$sqft=number_format(($sqm * 10.764), 0, ',', '\,');
return $sqft;
}
 ?>



code:
<?php echo calcsqft(400) . " sq ft" ?>



That works just fine. Now I would like to cut the code down to this:

code:
<?php echo calcsqft(400) ?>



How do I get the echo sq ft part into the function?

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 11-19-2009 17:54

return $sqft;
return $sqft . " sq ft";

NetGhost
Obsessive-Compulsive (I) Inmate

From:
Insane since: Nov 2009

posted posted 11-20-2009 19:13

Thanks Tyberius! I didn't think it would be that simple.

I have optimized the script further:

code:
<?php
function sqft($sqm){
$ft=number_format(($sqm * 10.764), 0, ',', '\,');
return number_format($sqm, 0, ',', '\'') . " m&sup2; / " . $ft . " sq ft";
}
 ?>



All that is required now is one instance of the square metre figure:

code:
<?php echo sqft(2700);?>



Output:

2'700 m² / 29,063 sq ft

« BackwardsOnwards »

Show Forum Drop Down Menu