martinspire Posted March 18, 2020 Share Posted March 18, 2020 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 Quote Link to comment https://forums.phpfreaks.com/topic/310314-converting-and-calculating-numbers-decimal-rounding/ Share on other sites More sharing options...
NotSunfighter Posted March 18, 2020 Share Posted March 18, 2020 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/310314-converting-and-calculating-numbers-decimal-rounding/#findComment-1575564 Share on other sites More sharing options...
ginerjm Posted March 18, 2020 Share Posted March 18, 2020 Your calculation produces 1350 which is square meters, but what 'unit' does 1.350 become? Quote Link to comment https://forums.phpfreaks.com/topic/310314-converting-and-calculating-numbers-decimal-rounding/#findComment-1575566 Share on other sites More sharing options...
martinspire Posted March 18, 2020 Author Share Posted March 18, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310314-converting-and-calculating-numbers-decimal-rounding/#findComment-1575568 Share on other sites More sharing options...
ginerjm Posted March 18, 2020 Share Posted March 18, 2020 So then NotSunFighter had the right approach. And using the number_format function would allow you to adjust the presentation. 1 Quote Link to comment https://forums.phpfreaks.com/topic/310314-converting-and-calculating-numbers-decimal-rounding/#findComment-1575569 Share on other sites More sharing options...
DavidAM Posted March 18, 2020 Share Posted March 18, 2020 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 1 Quote Link to comment https://forums.phpfreaks.com/topic/310314-converting-and-calculating-numbers-decimal-rounding/#findComment-1575576 Share on other sites More sharing options...
ginerjm Posted March 18, 2020 Share Posted March 18, 2020 But NotSunFighter has already solved the math for us. Quote Link to comment https://forums.phpfreaks.com/topic/310314-converting-and-calculating-numbers-decimal-rounding/#findComment-1575582 Share on other sites More sharing options...
Barand Posted March 18, 2020 Share Posted March 18, 2020 To round $x up to the nearest $y mm $x = ceil($x / $y) * $y; Quote Link to comment https://forums.phpfreaks.com/topic/310314-converting-and-calculating-numbers-decimal-rounding/#findComment-1575585 Share on other sites More sharing options...
DavidAM Posted March 19, 2020 Share Posted March 19, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310314-converting-and-calculating-numbers-decimal-rounding/#findComment-1575617 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.