2011-05-13 7 views
0

사용자가 부동 소수점 값을 입력 할 수있는 형식이 있습니다. 이 값을 PHP 스크립트에 게시하고 사용자가 입력 한 숫자가 일부 값 사이에 있는지 비교합니다. 내가 정수를 게시하면 숫자가 경계를 초과하는지에 관계없이 비교가 true를 반환합니다. 부동 소수점 숫자를 입력하면 숫자가 경계 내에 있는지에 관계없이 비교가 실패합니다. 여기 PHP 부동 소수점을 입력 필드 값과 비교합니다.

내 코드는 ... 내가, 내가 C++에서 소수점 비교 부동 한 적이 바보 아니에요 그리고 난 false를 반환 (float1> = float2) 경우 어떻게 해야할지 :

//loading the helper 
$val = Loader::helper('synerciel_form','synerciel_client'); 
//adding the fields to the inputs array for validation 
$val->between('isolation_des_murs_peripheriques', 2.8, null, t($between.'Isolation des murs 
pèriphèriques'), true, false); 

// 헬퍼 클래스

class SynercielFormHelper extends ValidationFormHelper { 
    const VALID_BETWEEN = 7; 
    const VALID_FLOAT = 7; 
    private $min; 
    private $max; 
    private $includeLow; 
    private $includeHigh; 

public function between($field, $min, $max, $errorMsg, $includeLow = true, $includeHigh = true) { 
     $const = SynercielFormHelper::VALID_BETWEEN; 
     $this->min = $min; 
     $this->max = $max; 
     $this->includeLow = $includeLow; 
     $this->includeHigh = $includeHigh; 

     $this->addRequired($field, $errorMsg, $const); 
    } 
    ... 
    public function test() { 

    $between = new ValidationNumbersBetweenHelper(); 
    if (!$between->between($this->data[$field], $this->min, $this->max, $this->includeLow, $this->includeHigh)) { 
         $this->fieldsInvalid[] = $f; 
} 

} 

내 검증 방법

class ValidationNumbersBetweenHelper { 

    public function between($data, $min = null, $max = null, $includeLow = true, $includeHigh = true) { 

     if ($min && $includeLow) { 
      if (!($data >= $min)) 
       return false; 
     } else if ($min) { 
      if (!($data > $min)) 
       return false; 
     } 
     if ($max && $includeHigh) { 
      if (!($data <= $max)) 
       return false; 
     } else if ($max) { 
      if (!($data < $max)) 
       return false; 
     } 
     return true; 
    } 

} 
+0

2 개의 이전 코드 단편이 추가되어서 어디에서 바이스와 전체 코드를 저장하는지 이해할 수 있습니다 흐름의 흐름 ... 나는 마지막 코드를 잘라낸 것이 당신이 관심을 가져야한다고 믿습니다. – user253530

답변

0

경고 체크 (내가 여기에 생각이 까다로운 부분입니다) 당신은 http://php.net/manual/en/function.bccomp.php

$status = bccomp($left, $right); 
if ($status == 0) { 
    echo 'equal'; 
} else if ($status > 0) { 
    echo 'left bigger than right'; 
} else { 
    echo 'right bigger than left'; 
} 

희망이

0

가 귀찮은 코드를 분리하려고하는 데 도움이 BC 수학 함수를 사용하여 메시지 http://php.net/manual/en/language.operators.comparison.php

을 보내고. 검증 기능을 독립 실행 형 PHP 파일에 넣고 테스트하십시오. 의 경우 $max$min을 확인하십시오. 0false입니다. 논리를 되돌리고 모든 !을 제거 할 수 있습니다. (예 : 변경 >= ~ <) "보다 크거나 같지 않음"대신 "미만"을 사용할 수 있습니다

관련 문제