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

    $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:
     
    Last edited by a moderator: Mar 13, 2014
    Tomve, Mar 13, 2014 IP
    sarahk likes this.
  3. ataloss

    ataloss Active Member

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

    ataloss Active Member

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    63
    #4
    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:
     
    ataloss, Mar 14, 2014 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #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 Active Member

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