SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    integer to float

    Hi,

    How can I change an integer value to float with 2 digits after the decimal places, in php.

    for example:

    i need to change 100 to be 1.00

    thanks

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    So, what about 123 - would that be 1.23? If so:
    PHP Code:
    $number number_format($number 1002); 

  3. #3
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I must ask why this sort of formatting is required. If it needs to be 1.00 then why isn't it received as such?
    Last edited by chris_fuel; Aug 24, 2007 at 11:18. Reason: Posted too slow :S

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've tried your code and customised it but it gives me 0.00 rather than 1.00

    PHP Code:
    $number number_format($number $_POST['PurchaseAmount'], 2); 
    In answer to Chris's question, this is how I recieve the amount from an external system (an online transaction processing system), I have no control over the formatting. The only way they can post it to me is as integer.

    Thanks

  5. #5
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That's because $number is supposed to be equal to $_POST['PurchaseAmount']. Also, you should check ("sanitize") that PurchaseAmount is a reasonable number, and a number at all.
    PHP Code:
    $pa $_POST['PurchaseAmount'];
    if (!
    is_numeric($pa)) {
      
    //error
    }
    else {
      
    $pa number_format($pa 1002);

    Dividing by 100 is what turns it from 100 to 1.00 or from 156 to 1.56 etc.

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    aha, i understand


    This works now.

    Many thanks


Bookmarks

Posting Permissions

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