php Number Formatting, Rounding, and Incrementing

February 10, 2010

updated 2019-09-15

Checking

ctype_digit()
only checks strings

from the manual:

$strings = array('1820.20', '10002', 'ws123');
foreach ($strings as $testcase) {
if (ctype_digit($testcase)) {
echo "The string $testcase consists of all digits.\n";
} else {
echo "The string $testcase does not consist of all digits.\n";
}
}

The above example will output:

The string 1820.20 does not consist of all digits.
The string 10002 consists of all digits.
The string ws123 does not consist of all digits.

$integer = 42;
ctype_digit($integer);
  // false

 

is_numeric()

Numeric strings contain any number of digits, optional signs such as + or -, an optional decimal, and an optional exponential.
Thus +0123.45e6 is a valid numeric value.
Hexadecimal (e.g. 0xf4c3b00c) and binary (e.g. 0b10100111001) notation is not allowed.

 

is_int() – Find whether the type of a variable is integer

is_int(23) = bool(true)
is_int(’23’) = bool(false)
is_int(23.5) = bool(false)
is_int(‘23.5’) = bool(false)
is_int(NULL) = bool(false)
is_int(true) = bool(false)
is_int(false) = bool(false)

 


Rounding

“The round() function rounds the number passed in to the specified number of decimal places. If the decimal places is a negative number then the numbers to the left of the decimal place are rounded. If the decimal places parameter is not passed in then it defaults to zero and will round as an integer. 5 is round up and < 5 is rounded down." examples:
echo round(153.751, 2); # 153.75
echo round(153.751, -2); # 200


number_format()


$number=2512589.66785;

echo number_format($number); # = 2,512,590 (without decimal and rounded)

echo number_format($number,3); # = 2,512,589.668
with , after each thousand and 3 digits after decimal rounded

echo number_format($number,3,"#",":"); # = 2:512:589#668
with : after each thousand and # as decimal symbol


printf() and sprintf()

the printf() function is used instead of echo

printf("%5.2f", $ttl);
forces 5 digets minimum in front and rounds to 2 after the decimal place no commas are added

echo sprintf("%1.2f", (5/3) ); # returns 1.67

Some common field templates are:
%s string
%d decimal number
%x hexadecimal number
%o octal number
%f float number

$num = 3;
echo sprintf(“%05d”, $num);
// returns 00003

echo sprintf(“$%2.2f”, 25.99);
// returns $25.99

echo sprintf("%'*6d", 56);
// returns ****56


echo “<h3>Postincrement</h3>”;
$a = 5;
echo “Should be 5: ” . $a++ . “<br>\n”;
echo “Should be 6: ” . $a . “<br>\n”;

echo “<h3>Preincrement</h3>”;
$a = 5;
echo “Should be 6: ” . ++$a . “<br>\n”;
echo “Should be 6: ” . $a . “<br>\n”;

echo “<h3>Postdecrement</h3>”;
$a = 5;
echo “Should be 5: ” . $a– . “<br>\n”;
echo “Should be 4: ” . $a . “<br>\n”;

echo “<h3>Predecrement</h3>”;
$a = 5;
echo “Should be 4: ” . –$a . “<br>\n”;
echo “Should be 4: ” . $a . “<br>\n”;

 

in case you encounter it, not advisable:

$i = ‘W’;
for ($n=0; $n<6; $n++) { echo ++$i . "\n"; } The above example will output: X Y Z AA AB AC

 

 

Leave a Reply

We try to post all comments within 1 business day