Note that there are some explanatory texts on larger screens.

plurals
  1. POInteger division in PHP produces zero - how to cast it to float?
    primarykey
    data
    text
    <p>In <a href="http://preferans.de" rel="nofollow noreferrer">a Russian card game</a> I'm trying to keep statistics of how often a player swears (says "bad words" - and we do have a lot of them in Russian language) in the following PostgreSQL table:</p> <pre><code># select * from pref_chat order by swear desc; id | swear | lines -------------------------+-------+------- OK194281930260 | 3 | 153 OK350321778615 | 2 | 127 DE12770 | 2 | 339 OK122898831181 | 2 | 63 OK349829847011 | 2 | 126 OK215240745969 | 1 | 66 OK459742722980 | 1 | 96 </code></pre> <p>And I need to generate an integer number of this data - between 1 and 100 (overflowing is okay) - so that I can create the following <em>"swear'o'meter</em>:</p> <p><img src="https://i.stack.imgur.com/TO9na.png" alt="enter image description here"></p> <p>So I'm trying (with PHP 5.3 at CentOS 6.2):</p> <pre><code> $sth = $db-&gt;prepare('select swear, lines from pref_chat where id=?'); $sth-&gt;execute(array($id)); if ($row = $sth-&gt;fetch(PDO::FETCH_ASSOC)) { $quotient = 100 * floor(20 * $row['swear'] / (1 + $row['lines'])); print('&lt;p&gt;&lt;img src="https://chart.googleapis.com/chart?chs=700x100&amp;cht=gom&amp;chco=00FF00,FFFF00,FF0000&amp;chxt=x&amp;chxl=0:|Swearometer&amp;chd=t:' . $quotient . '" width=700 height=100 alt="Swearometer"&gt;&lt;/p&gt;)'; } </code></pre> <p>Unfortunately I get zero - because PHP is probably doing an "integer division".</p> <p>I've tried to prepend <strong>floor()</strong> to <strong>$row['swear']</strong> and <strong>$row['lines']</strong> to "cast" them to float - but this didn't help.</p> <p><strong>UPDATE:</strong></p> <p>Sorry, I had a typo in my original question... The $quotient is really 0, I print it out. I've also tried the following, but still zero:</p> <pre><code>$quot = 100 * floor(20 * $row['swear'] / (.1 + $row['lines'])); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload