Jump to content

Variable and Value Types


yarivcohen

Recommended Posts

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 ?

 

Link to comment
Share on other sites

$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)
Link to comment
Share on other sites

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
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.