Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Apr 2008
    Posts
    24
    Thanks
    8
    Thanked 0 Times in 0 Posts

    function format_number

    I have an order form that originally had 4 digits after the decimal place. We changed it to 2, but now the price rounds up (for example, if the price is $65.34, it shows $66.00 on the order form - but once the data is transferred to the database, it shows the exact dollar amount.)

    What do I need to change in this code in order for the price not to round up on the order form?

    function format_number($str,$decimal_places='2',$decimal_padding="0"){
    $str = number_format($str,$decimal_places,'.',''); // will return 12345.67
    $number = explode('.',$str);
    $number[1] = (isset($number[1]))?$number[1]:''; // to fix the PHP Notice error if str does not contain a decimal placing.
    $decimal = str_pad($number[1],$decimal_places,$decimal_padding);
    return (float) $number[0].'.'.$decimal;
    }

    Thanks in advance for your assistance.

  • #2
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    I did a quick remake of yours.. erm here it is.

    PHP Code:
    <?php

    function format_number($number$decimal "2"$padding "0")
    {
      if (
    $decimal != -1)
      {
        
    $number number_format($number$decimal'.'' ');
      }

      if (
    $decimal == && $padding != 0)
      {
        
    $number .= ".";
      }

      
    $number str_pad($numberstrlen($number)+$padding"0");
      return 
    $number;
    }

    echo 
    format_number("65.3456", -1)."<br />"// Output = 65.3456
    echo format_number("65.3456"02)."<br />"// Output = 65.00
    echo format_number("65.3456"2)."<br />"// Output = 65.35
    echo format_number("65.3456"0)."<br />"// Output = 65

    ?>
    I think you get the idea...

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    you can use sprintf:

    PHP Code:
    $value 63.1456;
    // padding with 0, minim width 1, 2 decimals
    echo sprintf("%01.2f",$value); 
    http://www.php.net/manual/en/function.sprintf.php

    regards

  • #4
    New Coder
    Join Date
    Apr 2008
    Posts
    24
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Hi Iszak,
    First of all, thank you for your willingness to help. It is deeply appreciated.

    When I substituted the old code for what you had, not only did it place the output numbers at the top of the order form page, but it also deactivated the database query to pull in the prices. Why is this?

  • #5
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    We'll have to see the code. But the output at the top of the page wouldn't happen.. unless you added the echo parts.. which was just an example.. As for the deactivation of the database queries I don't see why that would happen.. again we need the code.

  • #6
    New Coder
    Join Date
    Apr 2008
    Posts
    24
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Iszak, here is the original code:

    PHP Code:
    <?php

    //mysql_connect ('localhost', 'root', 'pw');        
    //mysql_select_db('dbname')



    mysql_connect("localhost","root","pw");
    mysql_select_db("dbname");


    function 
    format_number($str,$decimal_places='2',$decimal_padding="0"){
           
    /* first format number and shorten any extra decimal places */
           /* Note this will round off the number pre-format $str if you dont want this functionality */
           
    $str           =  number_format($str,$decimal_places,'.','');     // will return 12345.67
           
    $number       explode('.',$str);
           
    $number[1]     = (isset($number[1]))?$number[1]:''// to fix the PHP Notice error if str does not contain a decimal 

    placing.
           
    $decimal     str_pad($number[1],$decimal_places,$decimal_padding);
           return (float) 
    $number[0].'.'.$decimal;
    }

    ?>


  •  

    Posting Permissions

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