戻る

数値を扱うPHP関数

max()関数、min関数

単体の数値の場合

最大値 = max(数値,数値[,数値...]);

mixed max ( mixed $value1 , mixed $value2 [, mixed $... ] )

最小値 = min(数値,数値[,数値...]);

mixed min ( mixed $value1 , mixed $value2 [, mixed $... ] )

例:

<?php
header("Content-type: text/html; charset=utf-8");

$a = 100;
$b = 2;
$c = 55;

echo max($a, $b, $c);
echo min($a, $b, $c);
?>

1002

数値が含まれる配列の場合

最大値 = max(配列);

mixed max ( array $values )

最小値 = min(配列);

mixed min ( array $values )

例:

<?php
header("Content-type: text/html; charset=utf-8");

$a = array(100,2,55);

echo max($a);
echo min($a);
?>

1002

ceil関数、floor関数、round関数

切り上げ後の整数値 = ceil(丸め前の数値);

		float ceil ( float $value )

切り捨て後の整数値 = floor(丸め前の数値);

		float floor ( float $value )

四捨五入後の数値 = round(丸め前の数値[,小数点以下の桁数]);

		float round ( float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]] )

例:

<?php
header("Content-type: text/html; charset=utf-8");

$value = 10.35;

echo ceil($value);
echo floor($value);
echo round($value);
?>

111010

小数第1位で四捨五入

<?php
header("Content-type: text/html; charset=utf-8");

$value = 10.35;

echo round($value, 1);
?>

10.4

10の位で四捨五入

<?php
header("Content-type: text/html; charset=utf-8");

$value = 353.42;

echo round($value, -2);
?>

400

ceil()関数やfloor()関数で小数点以下を丸める

小数点以下第1位に丸めるとき
10を掛けて、10で割る
小数点以下第2位に丸めるとき
100を掛けて、100で割る
小数点以下第3位に丸めるとき
1000を掛けて、1000で割る

例:

<?php
header("Content-type: text/html; charset=utf-8");

$value = 353.42;

echo round($value, -2);
?>

10.410.3

rand()関数

乱数 = rand([,最小値][,最大値])

int rand ( void )
int rand ( int $min , int $max )

例:

<?php
header("Content-type: text/html; charset=utf-8");

$a = rand(1,100);
echo $a . "<br>";

$a = rand(1,100);
echo $a . "<br>";

$a = rand(1,100);
echo $a . "<br>";
?>

70
77
19

number_format()関数

フォーマットされた文字列 = number_format(数値[,少数の桁数][,少数点の文字][,桁区切り文字])

string number_format ( float $number [, int $decimals = 0 ] )
string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )

例:

<?php
header("Content-type: text/html; charset=utf-8");

$value = 123456789.0123456;

echo number_format($value);
?>

123,456,789

<?php
header("Content-type: text/html; charset=utf-8");

$value = 123456789.0123456;

echo number_format($value,4);
?>

123,456,789.0123

<?php
header("Content-type: text/html; charset=utf-8");

$value = 123456789.0123456;

echo number_format($value,4,"-","=");
?>

123=456=789-0123

inserted by FC2 system