嗨,大家好,這裏只需要一個小小的幫助。右邊的綠色數字是字符串。我如何將它們更改爲數字?另外我還需要它們是2位小數。我使用什麼功能?我嘗試了下面的方法,但輸出爲0.答案都歡迎。
$profitText = $profitText*1;
$profitText = (float)$profitText;
round($profitText,2);
number_format($profitText, 2);
EDITED 好男人這個變量的推導是非常複雜的。每一步都有它的功能目的,但是繼承了它的派生。在底部的最後一個profitText之後,我意識到這是一個字符串。爲什麼?我該如何解決它?
$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');
您不一定需要將它們轉換爲數字 - 如果您在預期使用數字的位置使用字符串,它會將其轉換爲您的數字:http://www.php.net/manual /en/language.types.string.php#language.types.string.conversion – andrewsi
看看這個..... http://stackoverflow.com/questions/2540078/convert-a-string-to-a-double-is-this-possible – user2366842
嘗試使用(浮點數)$ numbemVar進行投射或讀取此http:// www .php.net/manual/en/function.number-format.php – PiLHA