[ノート]『PHP Cookbook 』を読む(CH02.数字)

```php
<?php

//変数は数字なのかを検査する
foreach ([5, '5', '05', 12.3, '16.7', 'five', 0xDECAFBAD, '10e200'] as $maybeNumber){
    $isNumeric=is_numeric($maybeNumber);
    $actualType=gettype($maybeNumber);
    print "Is the $actualType $maybeNumber numeric?";
    if($isNumeric){
        print "yes";
    }else{
        print "no";
    }
    print "\n";
}

//is_numericを使う前に、str_replaceで','を取り除く。
$number="5,100";
$withCommas = is_numeric($number);
$withoutCommas = is_numeric(str_replace(',', '', $number));
var_dump($withCommas);
var_dump($withoutCommas);

//floatを比較する
$delta = 0.00001;
$a = 1.00000001;
$b = 1.00000000;
if (abs($a - $b) < $delta) {
    print '$a and $b are equal enough.';

}
print "\n";

//floatを整数に 1.最も近い整数に
$number=round(2.4);
var_dump($number);
printf("2.4 rounds to the float %s", $number);
//二つの整数の真ん中にある場合、０から離れた場所で整数を取る。
$number = round(2.5);
printf("Rounding a positive number rounds up: %s\n", $number);
$number = round(-2.5);
printf("Rounding a negative number rounds down: %s\n", $number);

print "\n";
$number=round(3.6);
var_dump($number);
printf("3.6 rounds to the float %s", $number);
print "\n";
//２.大きい整数に
$number=ceil(2.4);
var_dump($number);
printf("2.4 rounds to the float %s", $number);
//3.小さい整数に
print "\n";
$number=floor(2.4);
var_dump($number);
printf("2.4 rounds to the float %s", $number);

//roundを使うとき、精度をパラメーターとして引き渡すこともできる
$cart = 54.23;
$tax = $cart * .05;
$total = $cart + $tax;
$final = round($total, 2);
print "Tax calculation uses all the digits it needs: $total, but ";
print "round() trims it to two decimal places: $final";
print "\n";

//整数範囲内でなんらかの処理を行う
$number=range(3,7);
foreach ($number as $n) {
    printf("%d squared is %d\n",$n,$n*$n);
    printf("%d cubed is %d\n",$n,$n*$n*$n);
}
print "\n";
//step を設定
$number=range(3,7,2);
foreach ($number as $n) {
    printf("%d squared is %d\n",$n,$n*$n);
    printf("%d cubed is %d\n",$n,$n*$n*$n);
}
print "\n";
//range で文字配列をとる
$array=range('l','p');
print_r($array);
print "\n";

//数字をランダンムに生成
$lower=65;
$upper = 97;
$random_number = mt_rand($lower, $upper);
print $random_number;
print "\n";

echo "-------偏向性があるランダンム数を生成 basic_sample_1--------\n";
//偏向性があるランダンム数を生成

function rand_weighted($numbers){
    $total=0;
    foreach ($numbers as $number=>$weight){
        $total+=$weight;
        $distribution[$number]=$total;
    }
    $rand=mt_rand(0,$total-1);
    foreach ($distribution as $number=>$weights){
        if($rand<$weight){
            return $number;
        }
    }
}

//数字の書式設定
$number = 1234.56;
$formatted=number_format($number);
print $formatted;
print "\n";
$formatted2 = number_format($number, 2);
print $formatted2;
print "\n";
$formatted3 = number_format($number, 1, ".", ",");
print $formatted3;
print "\n";

//decimalsは分からない場合
$number=31415.92653;
$arr=explode('.',$number);
var_dump($arr);
list($int,$dec)=explode('.',$number);
var_dump($int);
var_dump($dec);
$formatted=number_format($number,strlen($dec));
var_dump($formatted);
print "\n";


echo "-------貨幣をローカライズ--------\n";
//貨幣をローカライズ
$number=1234.56;
var_dump($number);
$jpy=new NumberFormatter("ja_JP",NumberFormatter::CURRENCY);
//小数を２位まで保留
$jpy->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, 2);
$formatted=$jpy->format($number);
var_dump($formatted);
print "\n";



//複数を出力
$number=4;
print "Your search returned $number".($number==1?'hit':'hits').".";

```