2017-11-05 15 views
1

私はパーセンテージの論理的な問題があり、対処方法は分かりません。PHPでのパーセンテージの扱い

年金収入(私が想定している)が現在の収入の70%未満であるかどうかを確認する必要がある場合があります。そうであればフラグを設定します。すべての良い。他の方法は、それを取得する方法を知らないので、テストが動的に与えられているだけで、私はこの割合を見つける機能を作成しました

Given my income on my pension is <percentage>% of my current 
Then the status should be <status> 

Example: 
|percentage| status | 
|100  | true | 
|71  | true | 
|68  | false | 
|20  | false | 

:この問題は、私は次のようにテストがどのように見えるべきかの例を受け取ったことです値:

public function findPensionIncomePercentageFromTheCurrent() 
{ 
    $pensionIncome = $this->getPensionIncome(); 

    $totalCurrentIncome = $this->getTotalCurrentIncome(); 

    if ($pensionIncome !== 0 && $totalCurrentIncome !== 0) { 
     $percentage = (int) round(($pensionIncome/$totalCurrentIncome) * 100, 0); 

     return $percentage; 
    } 

    return false; 
} 

これはパーセンテージです。私はまた、現在の収入から70%を計算する別の関数を作成しました。そして最後に、上記の関数の割合と年金収入の70%の値を比較しようとしました。しかし、私はそれは私が再びのような現在の収入割合掛ける場合にのみ動作します実現:

$currentIncome = $this->getTotalCurrentIncome(); 

$70percentageOfCurrentIncome = 70% * $currentIncome; 

$result = $this->findPensionIncomePercentageFromTheCurrent() * $currentIncome; 

if ($result < $70percentageOfCurrentIncome) 
    $this->setTrue(true); 
else { 
    $this->setFalse(false); 

をあなたはそれが私がそれをどうやっ大丈夫だと思いますか?結果を得るためにa/b * 100を実行してパーセントを見つけるためにちょっと変わったことがあり、その次に再びbを乗算するために尋ねています。私はうまくやっていないと思う。

提案がありますか?

+4

[数値の割合はどのように計算しますか?](https://stackoverflow.com/questi) ons/10201027/how-do-i-calculate-of-a-number) –

+0

これは実際に動作しますか? '$ 70percentageOfCurrentIncome = 70%* $ currentIncome;'はあらゆる種類のエラーを投げるように見える – apokryfos

+0

@apokryfosは心配なくそれを返します:return(int)round(($ percentage/100)* $ salaryIncome、0);どこのパーセンテージが70 – IleNea

答えて

1

本当に技術的になるためには、パーセンテージは割合に対応する数字です。

したがって、所得が1000で年金が300の場合、収入に対する年金の割合は、0.3であり、30%ではありません。

public function findPensionIncomePercentageFromTheCurrent() 
{ 
    $pensionIncome = $this->getPensionIncome(); 

    $totalCurrentIncome = $this->getTotalCurrentIncome(); 

    if ($pensionIncome !== 0 && $totalCurrentIncome !== 0) { 
     $percentage = ($pensionIncome/$totalCurrentIncome); 

     return $percentage; 
    } 

    return false; 
} 

を次にそれがの問題で、次のようになります:ここで

はあなたがやるべきものだあなたはあなたがする必要がある人々にパーセンテージを表示する必要がある場合

$currentIncome = $this->getTotalCurrentIncome(); 

//0.7 is the real percentage value, 
//70% is just something people use because we like whole numbers more 
$threshold = 0.7 * $currentIncome; 

$result = $this->findPensionIncomePercentageFromTheCurrent() * $currentIncome; 

if ($result < $threshold) 
    $this->setTrue(true); 
else { 
    $this->setFalse(false); 

今の注意点はあります次のようなものがあります。

echo round($percentage*100). "%"; //Would print something like 70% 
+0

ありがとう、私はそれを得た – IleNea

関連する問題