A small problem

Hello
I have gone to make a kind of calculator and i have all the inputs correct, but there is an issue when it comes to the operation of the result which is done by varialbes $a=$_POST[‘weight’]; and $b=$_POST[‘height’];

Your result is <?php echo $a/$b*$b;?>. //here I need to make $b multiplated by $b BEFORE $a is divided by multiplication of $b s. i tried with puting it $a/($b*$b); but it did not work.
I appreciate all the help of yours

[php]<?php
$a=5;
$b=5;
$m= $b*$b;
$d=$a/$m;
echo “Result $d”;
?>[/php]

Please click Karma by my name

seems to work just fine here.

[php]<?php
$result = null;

if (!empty($_POST[‘weight’]) && !empty($_POST[‘height’])) {
$a = $_POST[‘weight’];
$b = $_POST[‘height’];
$result = $a/($b*$b);
}
?>

Submit <?php

if (!empty($result)) { ?>
Weight: <?= htmlentities($a) ?>

Height: <?= htmlentities($b) ?>

Calc: weight/(heightheight) = <?= htmlentities($a) ?>/(<?= htmlentities($b) ?><?= htmlentities($b) ?>) = <?= htmlentities($a) ?> / <?= $b*$b ?>

Your result is <?= $result ?>.<?php
}[/php]

Output:

Weight: 100 Height: 5 Calc: weight/(height*height) = 100/(5*5) = 100 / 25 Your result is 4.

If this script doesn’t work for you then try to add error reporting to the script and see if you get any errors. Add this to the beginning of your script:
[php]ini_set(‘error_reporting’, E_ALL);
ini_set(‘display_errors’, ‘1’);[/php]

Thanks JimL, very helpful. Thank you aswell benanamen…
:slight_smile:
Also sorry if i am a complicator but height is tought to be a decimal number and then whetever decimal number i type it recognizes it as 1 (1,9;1,6;1,7;):frowning: Is there a solution for it?

Weight: 100 Height: 1,9 Calc: weight/(height*height) = 100/(1,9*1,9) = 100 / 1 Your result is 100.
THanks

[size=14pt]Here is a clean one liner to handle decimals up to two places.[/size]

[php]<?php
$a=100;
$b=5;
echo number_format($a/($b*$b), 2);
?>[/php]

Float numbers handle decimals, and the “correct” notation for decimals is 1.9

[php]<?php
$result = null;

if (!empty($_POST[‘weight’]) && !empty($_POST[‘height’])) {
$a = (float) str_replace(’,’, ‘.’, $_POST[‘weight’]); // cast as float and convert comma-separator to dot
$b = (float) str_replace(’,’, ‘.’, $_POST[‘height’]); // cast as float and convert comma-separator to dot
$result = $a/($b*$b);
}
?>

Submit <?php

if (!empty($result)) { ?>
Weight: <?= htmlentities($a) ?>

Height: <?= htmlentities($b) ?>

Calc: weight/(heightheight) = <?= htmlentities($a) ?>/(<?= htmlentities($b) ?><?= htmlentities($b) ?>) = <?= htmlentities($a) ?> / <?= $b*$b ?>

Your result is <?= $result ?>.<?php
}[/php]

Output:

Weight: 5 Height: 1.9 Calc: weight/(height*height) = 5/(1.9*1.9) = 5 / 3.61 Your result is 1.3850415512465.

Pretty sure he does not need a result out to thirteen decimal places. Being that he is dealing with a height and weight he appears to be trying to come up with a shipping cost such as for UPS or the like. If it is regarding shipping, he may even be required to round up to get a correct rate. So with your example he might actually need 1.39 as his return value

FEDEX: If the dimension includes a fraction of .5 or greater, it will be rounded up to the next whole number. If the dimension includes a fraction less than .5, it will be rounded down to the next whole number.

UPS: Round any fraction of a pound to the next whole pound

US Postal mail:
a. Round off requires increasing by 1 the last digit to be kept if the digit to its right, which is not to be kept, is 5 or greater. If that digit is 4 or less, the last digit kept is unchanged (e.g., 3.376 rounded off to two decimal places is 3.38, 3.374 is 3.37).

b. Round up requires increasing by 1 the last digit to be kept if there are any digits to its right, regardless of significance (e.g., rounding up either 3.3701 or 3.379 to two decimal places yields 3.38).

**kaktus200113 - What are you using this calculation for?

Yeah but the question was on the calculation, doing a number format on output is trivial. And since he hadn’t mentioned any spesific format it was easier to just output the full number ^^

THank you for all the help again, and I am just freaking up, height and weight could be anything :smiley:
will give karma in an hour when its available

i know its crude but wouldn’t this work ?
[php]<?php
if ($shipper = FEDEX){
$rnd= ‘PHP_ROUND_HALF_UP’;
}elseif ($shipper = UPS){
$rnd= ‘PHP_ROUND_HALF_EVEN’;
}elseif ($shipper = USPS){
$rnd= ‘PHP_ROUND_HALF_UP’;
}

$result = round($a/($b*$b),1,$rnd);
?>[/php]

I love seeing everyone take on a problem.

thanks i try to help where i can even though i am not an ace like the rest of ya’s

but wouldn’t using just round($var) be implicit of JimLs 13 decimal places.

where round($var,1,2) would stop at 2 decimals? (if i am reading the php doc correctly.

Yes, round($var) is exactly what I was going to last post until I re-read the UPS spec. UPS is only a round up situation and not a possible up or down round like others. round($var) will go up and down.

THanks for help again everyone, just why is it important what do i use calculation for? I am learning php and it was a practice. Not sure how moderation works here, but thread can be closed.

Quite normal to want to know the use case, allows to suggest other implementations. Also a effective XY problem killer

Haaa Haaa! I didnt know it had name but I have very much experienced it, even as recent as yesterday from this website. Someone had asked about adding a triple connected drop down to their site. Easy enough. I said just pay me what ever its worth to you since I would have done it for free if I was asked. After over two hours of collaboration it turned out he needed a full on database application and didn’t even have a site or the database or anything. :o

After going through all the motions of giving a real bid and estimated completion no word back. The XY Problem is exactly why I dont bid on jobs on freelancer. There is not near enough details provided for the jobs to even think about offering a bid.

Sponsor our Newsletter | Privacy Policy | Terms of Service