Hello, "amtdue" and "paidamt" are decimal (7,2). How do I get "$due" to consistently present decimal (7,2)? $due = $row['amtdue'] - $row['paidamt']; echo "<td align=right>$due</td>";
Hi, it's necessary to use dot instead of comma for decimals. So you could use str_replace function (http://www.php.net/manual/en/function.str-replace.php) Result is something like this $amtdue = str_replace(',', '.', $row['amtdue']); $paidamt = str_replace(',', '.', $row['paidamt']); $due = $amtdue - $paidamt; echo '<td align=right>'.$due.'</td>'; PHP: btw if you need $due with comma add this on line 5: $due = str_replace('.', ',', $due); PHP:
Thanks guys.No change; I get decimals every 3 or 4 lines. all entries in the dabase table are decimal(.00) $$totdue = $totdue + $row['amtdue']; $totpaid = $totpaid + $row['paidamt']; $totowed = $totowed + $row['amtdue']; PHP: // (http://www.php.net/manual/en/function.str-replace.php) $amtdue = str_replace(',', '.', $row['amtdue']); $paidamt = str_replace(',', '.', $row['paidamt']); $due = $amtdue - $paidamt; // $due = str_replace('.', ',', $due); PHP: Can this following be done w/str_replace? $late = $row['dayslate']; if ($late > 120) { $row['dayslate'] = "pastdue"; } PHP: // $row['duedate'] = preg_replace('/^[0:-]*$/', '', $row['duedate']); // $row['datepaid'] = preg_replace('/^[0:-]*$/', '', $row['datepaid']); // if(str_replace(array('0','-',':'),' ',$row['dayslate'])=>'120');{$row['dayslate']='past due';} echo "<tr>"; echo "<td>" . $row['status'] . "</td>"; echo "<td>" . $row['acctno'] . "</td>"; echo "<td>" . $row['bname'] . "</td>"; echo "<td>" . $row['purpose'] . "</td>"; echo "<td>" . $row['duedate'] . "</td>"; echo "<td>" . $row['datepaid'] . "</td>"; echo "<td align=right>" . $row['dayslate'] . "</td>"; echo "<td align=right>" . $row['amtdue'] . "</td>"; echo "<td align=right>" . $row['paidamt'] . "</td>"; echo '<td align=right>'.$due.'</td>'; } PHP:
I think the other posters missed what you are asking for... which would be number_format. http://us3.php.net/number_format You are basically saying that decimal(7,2) is their format in the database, and you want PHP to output them in that format -- You are NOT saying that 7,2 is the data itself, which is what all the str_replace nonsense would be for. Right? echo '<td>', number_format($due, 2, '.', ''), '</td>'; Will make it always display 2 decimal places -- which is all you are asking for, correct? I'd do that at the output stage as PHP can switch it back to a normal float without warning. Also, in your last example, STOP using multiple Echo to do one echo's job... valid markup might help too. Single quotes instead of double quotes and comma delimits instead of string addition to keep the memory use under control... Oh, and your 'late' bit? Better done with a inline conditional $late = $row['dayslate'] > 120 ? 'pastdue' : $row['dayslate']; Oh, and did you really mean to use a variable variable here? $$totdue = $totdue + $row['amtdue']; You might also want to look into assignment operators: $totdue += $row['amtdue']; $totpaid += $row['paidamt']; $totowed += $row['amtdue']; functionally identical.