We will start with a bit more complex outcome for numbers :

Outcome :

To format numbers, you will have to switch from an ECHO command to a PRINTF command.

Outcome :

So how does this PRINTF work? The printf command tells the server to format the following variable in "this" way.

The important settings are between the % and the f. The first digit inserts zeros for extra padding which depends on the second digit. The second digit states how many padding spaces are going to appear before the decimal space. In the above example, the second digit is set at 1 so there will be at least one. Try setting it to 5 and see what happens :

Outcome :

The third digit states how many decimal places are going to be shown after the decimal. Try setting it to 4 :

Outcome :

ROUND is used to round a number up or down to the nearest number. 5 and over will roundup, 4 and under will round down.

Outcome :

To specify a certain number of decimal places, add in a comma and number after the variable (or number) in the brackets. The last number will be rounded up or down depending on the next decimal number.

Outcome :

The absolute function will create the positive effect to a number. If it is a positive number to start, it will remain positive. If it is a negative number to start, the result will be a positive number.

Outcome :

<?php

$first_number = 10;

$second_number = 3;

$div_value = $first_number / $second_number;

echo "$div_value <br />";

$total_sum = $first_number + $second_number;

echo "$total_sum";

?>

$first_number = 10;

$second_number = 3;

$div_value = $first_number / $second_number;

echo "$div_value <br />";

$total_sum = $first_number + $second_number;

echo "$total_sum";

?>

Outcome :

3.3333333333333

13

13

To format numbers, you will have to switch from an ECHO command to a PRINTF command.

<?php

$first_number = 10;

$second_number = 3;

$div_value = $first_number / $second_number;

printf ("%01.2f", $div_value);

echo "<br />";

$total_sum = $first_number + $second_number;

printf ("%01.2f", $total_sum);

?>

$first_number = 10;

$second_number = 3;

$div_value = $first_number / $second_number;

printf ("%01.2f", $div_value);

echo "<br />";

$total_sum = $first_number + $second_number;

printf ("%01.2f", $total_sum);

?>

Outcome :

3.33

13.00

13.00

So how does this PRINTF work? The printf command tells the server to format the following variable in "this" way.

The important settings are between the % and the f. The first digit inserts zeros for extra padding which depends on the second digit. The second digit states how many padding spaces are going to appear before the decimal space. In the above example, the second digit is set at 1 so there will be at least one. Try setting it to 5 and see what happens :

<?php

$first_number = 10;

$second_number = 3;

$div_value = $first_number / $second_number;

printf ("%05.2f", $div_value);

?>

$first_number = 10;

$second_number = 3;

$div_value = $first_number / $second_number;

printf ("%05.2f", $div_value);

?>

Outcome :

03.33

The third digit states how many decimal places are going to be shown after the decimal. Try setting it to 4 :

<?php

$first_number = 10;

$second_number = 3;

$div_value = $first_number / $second_number;

printf ("%05.4f", $div_value);

?>

$first_number = 10;

$second_number = 3;

$div_value = $first_number / $second_number;

printf ("%05.4f", $div_value);

?>

Outcome :

3.3333

ROUND is used to round a number up or down to the nearest number. 5 and over will roundup, 4 and under will round down.

<?php

$number_1 = 10.356;

$rounded_number_1 = round($number_1);

echo "$rounded_number_1 <br />";

$number_2 = 10.5367;

$rounded_number_2 = round($number_2);

echo "$rounded_number_2";

?>

$number_1 = 10.356;

$rounded_number_1 = round($number_1);

echo "$rounded_number_1 <br />";

$number_2 = 10.5367;

$rounded_number_2 = round($number_2);

echo "$rounded_number_2";

?>

Outcome :

10

11

11

To specify a certain number of decimal places, add in a comma and number after the variable (or number) in the brackets. The last number will be rounded up or down depending on the next decimal number.

<?php

$number_1 = 10.356;

$rounded_number_1 = round($number_1,1);

echo "$rounded_number_1 <br />";

$number_2 = 10.5367;

$rounded_number_2 = round($number_2,2);

echo "$rounded_number_2";

?>

$number_1 = 10.356;

$rounded_number_1 = round($number_1,1);

echo "$rounded_number_1 <br />";

$number_2 = 10.5367;

$rounded_number_2 = round($number_2,2);

echo "$rounded_number_2";

?>

Outcome :

10.4

10.54

10.54

The absolute function will create the positive effect to a number. If it is a positive number to start, it will remain positive. If it is a negative number to start, the result will be a positive number.

<?php

$number_1 = abs(35);

$number_2 = abs(-35);

echo "$number_1 <br />";

echo "$number_2";

?>

$number_1 = abs(35);

$number_2 = abs(-35);

echo "$number_1 <br />";

echo "$number_2";

?>

Outcome :

35

35

35