idan traders
idan traders

Reputation: 79

Get a minus or plus in number

I have some function in PHP where I do some queries in SQL and get a number from an user input.

And I stack there how its should be.

My code:

$x = $request->input('usd');
$roi = $profit / $request->input('usd');
$cor = $request->input('usd') - $profit;
$prof = abs($profit);

$data['roi'] = $roi >= 0 ? '+'. $roi : '-'. $roi;
$data['cor'] = $cor >= $x ? '+' . $cor : '-' . $cor;
$data['prof'] = $profit >= $x ? '+' . $prof : '-' . $prof;

Where for example $x = 1000$ But it always does a + in minus numbers and plus in - numbers.

UPDATE:

i did a debug in a php i get the minus

        $x = '1000';
        echo 'X: ' . $x . '<br />';
        $pro = '-0.59%';
        $roi = $pro / $x;
        echo 'ROI: ' . $roi . '<br />';
        echo sprintf('ROI: ',$roi);
        $cor = $pro - $x;
        echo 'COR: ' . $cor . '<br />';
        $prof = $pro;
        echo 'PROFIT: ' . $pro . '<br />';
        ?>

but if i print it in the AJAX its now show the minus. results:

X: 1000 ROI: -0.00059 ROI: COR: -1000.59 PROFIT: -0.59%

Upvotes: 0

Views: 232

Answers (1)

Eric Mayfield
Eric Mayfield

Reputation: 93

I suggest you do some debugging to trace your values. Whenever I have situations like this it is nice to see how each value is tracking along the way. You can use a debugging software such as http://phpdbg.com/ or you can always manually do debugging such as the following:

    $x = $request->input('usd');
    echo 'X:" $x . '<br>';
    $roi = $profit / $request->input('usd');
    echo 'ROI:" $x . '<br>';
    $cor = $request->input('usd') - $profit;
    echo 'COR:" $x . '<br>';
    $prof = abs($profit);
    echo 'PROF:" $x . '<br>';

    $data['roi'] = $roi >= 0 ? '+'. $roi : '-'. $roi;
    echo 'data[roi]:" $x . '<br>';
    $data['cor'] = $cor >= $x ? '+' . $cor : '-' . $cor;
    echo 'data[cor]:" $x . '<br>';
    $data['prof'] = $profit >= $x ? '+' . $prof : '-' . $prof;
    echo 'data[prof]:" $x . '<br>';

A debugger is obviously much more elegant, but sometimes for simple value tracing manual isn't so bad.

Upvotes: 1

Related Questions