유

기초 테스트2

round

주어진 값을 주어진 자리 아래에서 반올림 하여 반환
 
Returns the rounded value of val to specified precision
 
echo round(1.95583, 2);  // 1.96
?>
 
문제> round 를 사용하지 않고 위와 동일한 문제에 동일한 답이 나오도록 처리하세요.
제한시간 - 2시간

댓글 16개

ㅠㅠ 저만 풀면 좀 그런거 같은데
어때요.... 다 같은 답이 나오는 것도 아니고 ..
그냥 풀면 됩니다.
디자이너로써 그냥>>
if 소수점 3자리째의 수가 5이상이면 2자리수에 +1시켜서 두자리수 까지만 출력해라.

else 이면 그냥 소수점두자리수까지만 표현해라.

라고 답글달아봅니다 ㅋ
echo "1.96";

ㅋㅅㅋ......
이건 쉽네요... ((int)1.95583 * (10 ^ 2) +9))/ 100)
echo xround(1.95583, 2);
function xround($rtn,$slength) {
$temp=1;
for ($i=1; $i<=$slength; $i++) $temp=$temp*10;
$rtn=$rtn*$temp+0.5;
return ((int) $rtn/$temp );
}

검사는 안해봣음 --; 대충 맞을것 같음

---> 검사해봄 잘됨
제한시간도 생겼네요 ㅠㅠ
최대한 네이티브로 했는데 is_numeric 은 예외처리로 끼워넣은거니까 봐주세요.

function round_custom($num,$pos=1){

if((int)$pos < 0) return 0;

if(is_numeric($num) == FALSE){
return 0;
}

$pos_helper = 1;
for($i = 0;$i<$pos;$i++){
$pos_helper*=10;
}

$constant_int_helper = 10;
$temp_var1 = (int)(($num*$pos_helper*$constant_int_helper + (($num*$pos_helper*$constant_int_helper)%10>4?10:-10))/$constant_int_helper)/$pos_helper;

return $temp_var1;
}
echo ((int)((1.95583 * 100) + .9)) / 100;
echo (int)((1.95583 + 0.005) * 100) / 100;
function banOlim($num, $r){

$roundingplace = pow(10,$r);
$num = $num*$roundingplace;
$num = ceil($num);

return $num / $roundingplace;

}

echo banOlim(1.95583, 2);
pow라는 함수가 있군요..... 허허~! 몰라서 for문 돌림 --;
ceil 은 반올림이 아닌것으로 이상합니다만.

banOlim(1.95583, 4) 해보시면 올림으로 나옵니다.
<?
function xround($float, $precision=0) {
$num = pow(10, $precision);
return (int)(($float + (0.5 / $num)) * $num) / $num;
}

echo xround(1.95583, 2);
echo number_format(1.95583, 2);
printf("%0.2f", 1.95583);
sprintf("%01.2f", 1.95583);
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

8,187건
+
제목 글쓴이 날짜 조회
12-01-21 조회 3,399
12-01-21 조회 4,188
12-01-21 조회 3,589
12-01-21 조회 3,261
12-01-20 조회 3,141
12-01-20 조회 2,782
12-01-20 조회 3,546
12-01-20 조회 3,391
12-01-20 조회 2,786
12-01-20 조회 3,215
12-01-20 조회 6,306
12-01-19 조회 3,333
12-01-19 조회 4,984
12-01-19 조회 3,275
12-01-19 조회 3,268
12-01-19 조회 3,227
12-01-19 조회 2,844
12-01-18 조회 3,784
12-01-18 조회 2,996
12-01-18 조회 2,747
12-01-18 조회 3,318
12-01-18 조회 2,979
12-01-18 조회 3,156
12-01-17 조회 2,950
12-01-17 조회 4,044
12-01-17 조회 4,470
12-01-17 조회 3,338
12-01-17 조회 3,452
12-01-17 조회 3,193
12-01-17 조회 5,267
12-01-17 조회 3,060
12-01-16 조회 2,794
12-01-16 조회 3,649
12-01-16 조회 4,139
12-01-15 조회 2,949
12-01-15 조회 3,764
12-01-15 조회 3,715
12-01-14 조회 3,616
12-01-14 조회 3,756
12-01-14 조회 3,486
12-01-14 조회 1.1만
12-01-14 조회 3,698
12-01-14 조회 4,012
12-01-14 조회 2,971
12-01-14 조회 2,824
12-01-13 조회 2,965
12-01-13 조회 3,541
12-01-13 조회 4,623
12-01-13 조회 2,958
12-01-12 조회 4,267
12-01-12 조회 4,406
12-01-12 조회 3,412
12-01-12 조회 3,250
12-01-11 조회 2,838
12-01-11 조회 3,269
12-01-11 조회 3,487
12-01-11 조회 3,209
12-01-11 조회 3,210
12-01-11 조회 3,318
12-01-11 조회 3,537
12-01-11 조회 4,323
12-01-11 조회 3,398
12-01-11 조회 2,858
12-01-11 조회 1.2만
12-01-11 조회 3,186
12-01-10 조회 2,996
12-01-10 조회 3,714
12-01-10 조회 4,781
12-01-10 조회 3,463
12-01-10 조회 3,191
12-01-10 조회 2,932
12-01-10 조회 3,837
12-01-10 조회 3,109
12-01-10 조회 4,621
12-01-10 조회 2,982
12-01-10 조회 3,021
12-01-10 조회 3,223
12-01-10 조회 3,862
12-01-10 조회 2,926
12-01-10 조회 4,547
12-01-10 조회 2,941
12-01-10 조회 5,417
12-01-10 조회 5,588
12-01-10 조회 5,466
12-01-10 조회 5,364
12-01-09 조회 2,885
12-01-09 조회 2,814
12-01-09 조회 3,355
12-01-09 조회 3,872
12-01-09 조회 2,817
12-01-09 조회 2,982
12-01-09 조회 2,789
12-01-08 조회 7,127
12-01-08 조회 2,811
12-01-08 조회 7,216
12-01-08 조회 3,746
12-01-08 조회 3,331
12-01-08 조회 2,733
12-01-08 조회 2,894
12-01-08 조회 3,529