Jump to content
#FlattenTheCurve ×
martinspire

Converting and Calculating Numbers (decimal & rounding)

Recommended Posts

Hello. This is my first post here. I am not an expert developer but I generally manage to do what is asked of me. However, now and then I'm stumped, like now...

I have an online database system for our company in which staff can enter JOB related data, this includes 2 dimensions which they enter in millimetres (mm).

Here's an example

$dimension1 might be entered as 850
$dimension2 might be entered as 1447
$square_metres is the value I need to calculate.

Firstly, I need to round both dimensions up to the next 100mm so the 850 in this example would become 900 and the 1447 would become 1500.

Once I've done that, I need to calculate the square metres (900mm x 1500mm / 1000 was my first thought) but that raised the second thing; adding a decimal point in the result. 900 x 1500 / 1000 = 1350 but I need the result to be 1.350

I know my way around basic PHP code but I'm no expert by any stretch.

If anyone can help me understand how to get this calculation done I would be hugely appreciative.

Thank you

Martin

Share this post


Link to post
Share on other sites

I believe I would convert the mm entrees to meters first then get the square meters.

$dimension1 = 850/1000;

$dimension2 = 1447/1000;

$square_metres = $dimension1 * $dimension2;

The results would be 1.22995 square meters. You can use number_format() if you need to limit the number of decimal places.

  • Like 1

Share this post


Link to post
Share on other sites

Your calculation produces 1350 which is square meters, but what 'unit' does 1.350 become?

Share this post


Link to post
Share on other sites
1 minute ago, ginerjm said:

Your calculation produces 1350 which is square meters, but what 'unit' does 1.350 become?

That will actually be 1.35 sq metres.

In order to calculate the price, I needed to get the entries from mm to a sq metre value.

Perhaps my original post was incorrect.

Share this post


Link to post
Share on other sites

So then NotSunFighter had the right approach.  And using the number_format function would allow you to adjust the presentation.

  • Like 1

Share this post


Link to post
Share on other sites
7 hours ago, martinspire said:

Once I've done that, I need to calculate the square metres (900mm x 1500mm / 1000 was my first thought) but that raised the second thing; adding a decimal point in the result. 900 x 1500 / 1000 = 1350 but I need the result to be 1.350

Your math is off. There are 1,000 mm in a meter. But there are 1,000,000 square mm in a square meter. You should divide by 1,000,000 if you use the approach in your original post.

900mm * 1500mm / 1000000 = 1.35 sq meters

  • Like 1

Share this post


Link to post
Share on other sites

But NotSunFighter has already solved the math for us.

Share this post


Link to post
Share on other sites

To round $x up to the nearest $y mm

$x = ceil($x / $y) * $y;

 

Share this post


Link to post
Share on other sites
19 hours ago, ginerjm said:

But NotSunFighter has already solved the math for us.

I just thought it would be good to explain why the original calculation was not working.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.