LoginSignup
1
0

More than 1 year has passed since last update.

【PHP基礎復習12】空白で区切る、四捨五入、小数点以下表示

Posted at

[問題]
仲良しグループ3人組のテストの点数の平均点を、四捨五入して小数点以下第1位まで表示させなさい。
3人の点数は、半角スペースで区切って入力される。

コード

$input = fgets(STDIN);
$array = explode(" ",$input);
$total = round(array_sum($array)/3, 1);
echo number_format($total,1);

↓ 60 70 65と入力

結果

65.0

☆explode(区切り文字 , 入力文字)
→ 区切り文字で区切った文字列を配列に変換

☆round(値 , 小数点以下の桁数)
→ 四捨五入して指定した小数点以下の桁数で表示

☆number_format(値 , 小数点以下の桁数)
→ 指定した小数点以下の桁数まで表示

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0