1

enter image description here

Hi Guys , just need a tiny help here . the green numbers on the right are strings . how do I change them to numbers ? Additionally I also need them to be 2 decimal place. What function do I use?? I tried the method below but the output was 0. Answers all welcome.

$profitText = $profitText*1;
$profitText = (float)$profitText;
round($profitText,2); 
number_format($profitText, 2);

EDITED Okay guys the deriving of this variable is really complex. every step has its functional purpose but heres the derivation. After the last profitText at the bottom, I realised this is now a string. why is that so? and how do I fix it?

 $offeropen=$row['offerprice'];//1.3334
      $pips=$offerpricepl-$offeropen;//difference btw prices , eg. 0.0023
      $closedb=$offerpricepl;// nothing
      $pips1=round($pips, 6);// round to 6 decimal points
      $pips2 = str_replace('.', '', $pips1);// remove decimal
        if ($pips2<0)
      {
        $pips2 = str_replace('-', '', $pips2);// methodology for adjusting figures and negative values back
        $pips2 = ltrim($pips2, '0');
        $pips2 = -1 * abs($pips2);
      }
      else {
        $pips2 = ltrim($pips2, '0');// for triming 0 on the left
      }
      $pips3=$pips2/$minipipskiller;// methodology
    $ticksize= "0.0001";// FOR PROFIT AND LOSS
      $lot1 = "100000";
      $sizecalc=$row['size'] * $lot1;

        if ($row['type']=="buy")
      { 
        $profitandloss=$sizecalc*$ticksize*$pips3; //per TRADE
      }
      if ($row['type']=="sell")
      {
        $profitandloss=$sizecalc*$ticksize*$pips3; //per TRADE
      }

      $zero= '0';

      if($profitandloss<$zero) {
            $profitText = "<div style=\"color: red;\">$profitandloss</div>";
        } elseif ($profitandloss>$zero) {
            $profitText = "<div style=\"color: green;\">$profitandloss</div>";
        }
        // for profit and loss counting

        $profitText=ltrim($profitText,'0');
6
  • 1
    You don't necessarily need to convert them to numbers - if you're using a string where a number is expected, it'll convert it to a number for you: php.net/manual/en/… Commented Aug 20, 2013 at 16:27
  • take a look at this..... stackoverflow.com/questions/2540078/… Commented Aug 20, 2013 at 16:27
  • Try casting with (float)$numbemVar or read this php.net/manual/en/function.number-format.php Commented Aug 20, 2013 at 16:28
  • before you change the variable, what does var_dump($profitText) give? (please from view source in your browser, not from the HTML page) Commented Aug 20, 2013 at 16:46
  • after using code it becomes float 0 Commented Aug 20, 2013 at 16:55

6 Answers 6

1

Here's your problem:

$profitText = "<div style=\"color: red;\">$profitandloss</div>";

You're trying to turn $profitText into a number. It's actually a string of HTML, and PHP can't manage to work out what it's supposed to do with it, so when you cast it to a number, it's returning 0.

Solution:

Use $profitandloss instead

Sign up to request clarification or add additional context in comments.

2 Comments

Well, that looks like it's where the content for that column is coming from, so I assume it's the actual number you're working with, rather than a formatted version.
This is what im looking , thanks man! i checked var dump , yes it is the case.
1

this will do both requested points together:

$floatProfitVar = number_format($profit, 2, '.');

Comments

1

I think you need

floatval(mixed var)

http://www.php.net/manual/en/function.floatval.php

$profit = round(floatval(trim($profitText)), 2);

Proof: http://codepad.org/jFTqhUIk

4 Comments

what do you mean by the string is valid?
Specifically i'd wonder if there's blank space before or after the value. Try using ltrim and rtrim on the string to make sure its "clean" before attempting to cast it. EDIT: seems this is accounted for in the answer given.
@nigel - could you edit your question to include the code where you define your variables?
@user2366842 Well, trim() combines ´ltrim()´ and ´rtrim()´
1

The function you're looking for is sprintf or printf and has been designed for exactly that job:

printf('%.2F', '1.8'); # prints "1.80"

Demo: https://eval.in/44078

You can use string input for the float number parameter (F = float, locale independent) as well as integer or float input. PHP is loosely typed.

Use sprintf if you want to get back a string, printf prints directly.

Comments

0
$value = intval(floatval($profitText)*100)/100.0

Comments

0

I don't think this $profitText = $profitText*1; would work unless you first do $profitText = (float)$profitText;. Remember that you have to convert the string first before you can do any manipulation.

floatval(mixed $var) can also be used instead of typecasting but typecasting should work fine

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.