www.webdeveloper.com
Results 1 to 6 of 6

Thread: Problem in displaying number format with comma and two decimal places

  1. #1
    Join Date
    Oct 2011
    Posts
    350

    Problem in displaying number format with comma and two decimal places

    Good day!

    I use round syntax in displaying my computation and the result is correct. but it has no comma.
    Like for example the result is 3020, I want it to 3,020.00

    I have this syntax:

    PHP Code:
    $Amount round(($Hours/8)* $Rate2); 
    but when I tried number_format it displayed correct but I think the data or the amount is wrong because I sum amount in other earnings before i change the format, the output is 5524.28, but when I change my format of my amount the sum of other earnings and amount become 2507.88, so the data that get in amount is only 3.6.

    i only want is adding comma, with changing the real amount or data.

    Thank you so much....

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,154
    If I'm reading that correctly, you cannot do math with numbers formatted that way (with comma separators). Only use the number_format() for outputting "pretty" results to the user.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Oct 2011
    Posts
    350
    Quote Originally Posted by NogDog View Post
    If I'm reading that correctly, you cannot do math with numbers formatted that way (with comma separators). Only use the number_format() for outputting "pretty" results to the user.
    How can I add comma? without using number_format

    Thank you

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,154
    There is no reason I know of that you can't use number_format(). But any function you use that puts in thousands separators will create a string representation of a number. If you try to then use it as a number using PHP mathematical operators, it will cast that string to a numerical type, at which point things will go wrong. E.g. "123,456.78" would be cast to 123. The "trick" is to only use number format when you actually output something:
    PHP Code:
    $a 123.45;
    $b 1234567.890
    $result 
    $b $a;
    printf(
        
    "<p>%s / %s = %s</p>",
        
    number_format($b2'.'','),
        
    number_format($a2. '.'','),
        
    number_format($result2'.'','
    ); 
    If that is not the issue, then we'll need to see some actual code to help you debug.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    Perhaps this kind of function...
    PHP Code:
    function frm($n){
        
    $m=round($n*100)/100;
        while ((
    $o=preg_replace("`(\d)(\d\d\d)($|,|\.)`","$1,$2$3",$m))!=$m$m=$o;
        if (!
    preg_match("`\.`",$m)) $m.='.00';
        if (
    preg_match("`\.\d$`",$m)) $m.='0';
        return 
    $m;
    }
    echo 
    "<br>".frm(3020);
    echo 
    "<br>".frm(3020.5);
    echo 
    "<br>".frm(3020.567);
    echo 
    "<br>".frm(1987654);

    // to retrieve the numbers
    $n=frm(3020.5);
    $m=(double)str_replace(',','',$n);
    echo 
    "<br>".$m

  6. #6
    Join Date
    Oct 2011
    Posts
    350
    here is my actual code:

    PHP Code:
     $sql "SELECT w.EMP_ID, w.RATE, e.Hours FROM $ADODB_DB.wage w, $PAYROLL.earnings e WHERE w.EMP_ID = '$currentEmpID'";
      
    $RsEarnings $conn2->Execute($sql);

      
    $Rate      trim($RsEarnings->fields['RATE']);
      
    $Hours      trim($RsEarnings->fields['Hours']);
      
      
    $Hours substr($Hours05);
      
    $Hours str_replace(':''.'$Hours);
      

     
    $Amount round(($Hours/8)* $Rate2); 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles