yarivcohen Posted August 2, 2012 Share Posted August 2, 2012 hay all i have simple question about varible type in php i have two values in my array $row['DS'] // type :float (with one decimal like 12.2) $row['TC'] // type :float (with one decimal like 24.2) what im exctaly try to do in the make the above calcualtion $row['TC'] / $row['DS'] ( $row['DS'] need to be as intenger (without point,like 12) and the resulte should be with two decimal like (2.32) i tried to do it in that way //////// $DSF = number_format($row['DS'],0); $ConF = $row['TC'] / $DSF ; echo number_format($conF,2); /////// but it return me a worng cllculation . for example : ($row['DS'] = 59,009.3---> (after change the format is change to 59,009 $row['TC'] = 190.0 $ConF = 190.0 / 59,009 = it sohuld be 000.223 (something around this number ) and i expect the get 0 (after i change the format using number_format($conF,2) but instead of this the program return me the number 3.22 someone has idea how to get ride out of this ? Quote Link to comment https://forums.phpfreaks.com/topic/266592-variable-and-value-types/ Share on other sites More sharing options...
peipst9lker Posted August 2, 2012 Share Posted August 2, 2012 $row['DS'] // type :float (with one decimal like 12.2) $row['TC'] // type :float (with one decimal like 24.2) what im exctaly try to do in the make the above calcualtion $row['TC'] / $row['DS'] ( $row['DS'] need to be as intenger (without point,like 12) and the resulte should be with two decimal like (2.32) Use typecasting $var = ($row['TC'] / (int)$row['DS']); For the future please put code in tags! (remove the spaces in the brackets) Quote Link to comment https://forums.phpfreaks.com/topic/266592-variable-and-value-types/#findComment-1366260 Share on other sites More sharing options...
cyberRobot Posted August 2, 2012 Share Posted August 2, 2012 The following seems to work for me: <?php print $ConF = 190.0 / 59009; //output: 0.0032198478198241 print '<br />'; print number_format($ConF,2); //output: 0.00 print '<br /><br />'; print $ConF = 190.0 / 59009.3; //output: 0.0032198314502968 print '<br />'; print number_format($ConF,2); //output: 0.00 print '<br /><br />'; print $ConF = 190.0 / 5.2; //output: 36.538461538462 print '<br />'; print number_format($ConF,2); //output: 36.54 ?> Quote Link to comment https://forums.phpfreaks.com/topic/266592-variable-and-value-types/#findComment-1366262 Share on other sites More sharing options...
peipst9lker Posted August 2, 2012 Share Posted August 2, 2012 cyberRobot, you're just removing decimal places, TS needs to convert datatypes before calculating. Quote Link to comment https://forums.phpfreaks.com/topic/266592-variable-and-value-types/#findComment-1366267 Share on other sites More sharing options...
cyberRobot Posted August 2, 2012 Share Posted August 2, 2012 I was just showing that the calculations seem to work fine either way. Besides, wouldn't it be better to remove the decimals in the end? The result would be more accurate. Quote Link to comment https://forums.phpfreaks.com/topic/266592-variable-and-value-types/#findComment-1366270 Share on other sites More sharing options...
cyberRobot Posted August 2, 2012 Share Posted August 2, 2012 Ah, it's the comma added by number_format() that's giving you the bad result. <?php print '<br /><br />'; $row['DS'] = number_format(59009.3,0); $row['TC'] = 190.0; print $ConF = $row['TC'] / $row['DS']; //output: 3.2203389830508 ?> If you really need to remove the decimals before the calculation, you could use typecasting as peipst9lker suggested. Note that this won't round like number_format(). You could use sprintf() instead. <?php print '<br /><br />'; $row['DS'] = 59009.6; //<-- note the change to .6 print number_format($row['DS'],0); //output: 59,010 print '<br />'; print (int)$row['DS']; //output: 59009 print '<br />'; print sprintf("%01.0f", $row['DS']); //output: 59010 ?> Or you could just leave the decimal there until all calculations are complete. Then remove the extra decimals at the end. Quote Link to comment https://forums.phpfreaks.com/topic/266592-variable-and-value-types/#findComment-1366276 Share on other sites More sharing options...
Mahngiel Posted August 2, 2012 Share Posted August 2, 2012 $var = ($row['TC'] / (int)$row['DS']); $var is deprecating. In order to maintain backward compatibility with PHP 4, PHP 5 will still accept the use of the keyword var in property declarations instead of (or in addition to) public, protected, or private. However, var is no longer required. If you declare a property using var instead of one of public, protected, or private, then PHP 5 will treat the property as if it had been declared as public. Quote Link to comment https://forums.phpfreaks.com/topic/266592-variable-and-value-types/#findComment-1366406 Share on other sites More sharing options...
cyberRobot Posted August 2, 2012 Share Posted August 2, 2012 I think you're getting that confused with the use of "var" in classes. http://www.php.net/manual/en/language.oop5.properties.php $var is the name of the variable. Quote Link to comment https://forums.phpfreaks.com/topic/266592-variable-and-value-types/#findComment-1366409 Share on other sites More sharing options...
KevinM1 Posted August 2, 2012 Share Posted August 2, 2012 $var != keyword var Quote Link to comment https://forums.phpfreaks.com/topic/266592-variable-and-value-types/#findComment-1366411 Share on other sites More sharing options...
Mahngiel Posted August 2, 2012 Share Posted August 2, 2012 touche. just skimmed the thread. my bad. Quote Link to comment https://forums.phpfreaks.com/topic/266592-variable-and-value-types/#findComment-1366417 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.