1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

help with code

Discussion in 'PHP' started by ataloss, Mar 13, 2014.

  1. #1
    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>";
     
    ataloss, Mar 13, 2014 IP
  2. Tomve

    Tomve Active Member

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    2
    Trophy Points:
    70
    #2
    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

    PHP:
    1. $amtdue = str_replace(',', '.', $row['amtdue']);
    2. $paidamt = str_replace(',', '.', $row['paidamt']);
    3.  
    4. $due = $amtdue - $paidamt;
    5.  
    6. echo '<td align=right>'.$due.'</td>';
    btw if you need $due with comma add this on line 5:

    PHP:
    1. $due = str_replace('.', ',', $due);
     
    Last edited by a moderator: Mar 13, 2014
    Tomve, Mar 13, 2014 IP
    sarahk likes this.
  3. ataloss

    ataloss Member

    Messages:
    59
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #3
     
    ataloss, Mar 14, 2014 IP
  4. ataloss

    ataloss Member

    Messages:
    59
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #4
    Thanks guys.No change; I get decimals every 3 or 4 lines.
    all entries in the dabase table are decimal(.00)

    PHP:
    1.  
    2. $$totdue = $totdue + $row['amtdue'];
    3. $totpaid = $totpaid + $row['paidamt'];
    4. $totowed = $totowed + $row['amtdue'];
    5.  

    // (http://www.php.net/manual/en/function.str-replace.php)

    PHP:
    1. $amtdue = str_replace(',', '.', $row['amtdue']);
    2. $paidamt = str_replace(',', '.', $row['paidamt']);
    3. $due = $amtdue - $paidamt;
    4. // $due = str_replace('.', ',', $due);
    5.  
    Can this following be done w/str_replace?

    PHP:
    1. $late = $row['dayslate'];
    2. if ($late > 120)
    3. {  
    4. $row['dayslate'] = "pastdue";
    5. }
    PHP:
    1. // $row['duedate'] = preg_replace('/^[0:-]*$/', '', $row['duedate']);
    2. // $row['datepaid'] = preg_replace('/^[0:-]*$/', '', $row['datepaid']);
    3. // if(str_replace(array('0','-',':'),' ',$row['dayslate'])=>'120');{$row['dayslate']='past due';}
    4. echo "<tr>";
    5. echo "<td>" . $row['status'] . "</td>";
    6. echo "<td>" . $row['acctno'] . "</td>";
    7. echo "<td>" . $row['bname'] . "</td>";
    8. echo "<td>" . $row['purpose'] . "</td>";
    9. echo "<td>" . $row['duedate'] . "</td>";
    10. echo "<td>" . $row['datepaid'] . "</td>";
    11. echo "<td align=right>" . $row['dayslate'] . "</td>";
    12. echo "<td align=right>" . $row['amtdue'] . "</td>";
    13. echo "<td align=right>" . $row['paidamt'] . "</td>";
    14. echo '<td align=right>'.$due.'</td>';
    15.   }
     
    ataloss, Mar 14, 2014 IP
  5. deathshadow

    deathshadow Prominent Member

    Messages:
    6,382
    Likes Received:
    947
    Best Answers:
    155
    Trophy Points:
    395
    #5
    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.
     
    deathshadow, Mar 14, 2014 IP
  6. ataloss

    ataloss Member

    Messages:
    59
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #6
    thanks so much. I'm really learning a lot from you.
     
    ataloss, Mar 14, 2014 IP