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');
Lain
  • 2,166
  • 4
  • 23
  • 47
nigel
  • 101
  • 3
  • 4
  • 10
  • 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: http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion – andrewsi Aug 20 '13 at 16:27
  • take a look at this..... http://stackoverflow.com/questions/2540078/convert-a-string-to-a-double-is-this-possible – user2366842 Aug 20 '13 at 16:27
  • Try casting with (float)$numbemVar or read this http://www.php.net/manual/en/function.number-format.php – PiLHA Aug 20 '13 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) – hakre Aug 20 '13 at 16:46
  • after using code it becomes float 0 – nigel Aug 20 '13 at 16:55
  • view source in the browser. then tell use about the string with 37 characters each which those 37 exactly are. May guess is simple: You have HTML strings there. PHP can convert them to numbers and correctly does so: 0. Number zero. They start with `<`. So you now see that you are asking the wrong question? You perhaps are more interested to read: [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/q/3577641/367456) – hakre Aug 20 '13 at 17:07

6 Answers6

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

Max
  • 292
  • 2
  • 7
  • what do you mean by the string is valid? – nigel Aug 20 '13 at 16:38
  • 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. – user2366842 Aug 20 '13 at 16:38
  • @nigel - could you edit your question to include the code where you define your variables? – andrewsi Aug 20 '13 at 16:39
  • @user2366842 Well, trim() combines ´ltrim()´ and ´rtrim()´ – Max Aug 20 '13 at 16:42
1

this will do both requested points together:

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

MaveRick
  • 1,181
  • 6
  • 20
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.

hakre
  • 193,403
  • 52
  • 435
  • 836
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

andrewsi
  • 10,807
  • 132
  • 35
  • 51
  • 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. – andrewsi Aug 20 '13 at 17:17
  • This is what im looking , thanks man! i checked var dump , yes it is the case. – nigel Aug 20 '13 at 17:25
0
$value = intval(floatval($profitText)*100)/100.0
Jiminion
  • 5,080
  • 1
  • 31
  • 54
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

thedevlpr
  • 1,101
  • 12
  • 28