SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member sayeek's Avatar
    Join Date
    Oct 2009
    Location
    Dhaka, Bangladesh
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post Money Format In PHP

    PHP Code:
    <?php
    function my_money_format($num){
        
    $money=explode('.',$num);
        if(
    strlen($money[1])==0)
            
    $money[1]='00';
        if(
    strlen($money[0])==0)
            
    $money[0]='0';    
        if(
    strlen($money[0])>2){
            
    $taka=$money[0];
            
    $thousand=substr($taka, -3);
            
    $taka=substr($taka,0,strlen($taka)-3);
            
    $i=0;
            while(
    strlen($taka)>0){
                if(
    strlen($taka)>1){
                    
    $pp[$i]=substr($taka, -2);
                    
    $taka=substr($taka,0,strlen($taka)-2);
                }else{
                    
    $pp[$i]=substr($taka, -1);
                    
    $taka=substr($taka,0,strlen($taka)-1);
                }
                
    $i++;
            }
            for(
    $j=sizeof($pp)-1;$j>=0;$j--)
                
    $taka_add .=$pp[$j].',';
            return 
    $taka_add.$thousand.".".$money[1];
        }else
            return 
    $money[0].".".$money[1];    
    }    
    ?>
    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    <form name="form1" id="form1" action="index.php" method="post">
    <input name="f" id="f" type="text" value="<?php echo $_POST['f'];?>" />
      <input type="submit"  name="Submit" value="Format" class="button" />
                    
      <input name="access" type="hidden" id="access" value="1" ></div>
      <br><br><br><br><br>
    </form>
    PHP Code:
    <?php
    $num
    =$_POST['f'];
    echo  
    my_money_format($num);
    ?>
    HTML Code:
    </body>
    </html>

    <snip/>
    Last edited by ScallioXTX; Mar 10, 2012 at 06:21. Reason: no self promo

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,096
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Okay, so what's wrong with PHP's money_format function?
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or even better intl's NumberFormatter

  4. #4
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    6 Thread(s)
    Off Topic:

    Ren nice to hear from you! Steve
    ictus==""

  5. #5
    SitePoint Member sayeek's Avatar
    Join Date
    Oct 2009
    Location
    Dhaka, Bangladesh
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Money Format

    If you use my script then you got output like that

    Input = 152469854
    Output= 15,24,69,854

    Input= 12541362547589
    Output=1,25,41,36,25,47,589

    My script is simple and easy.

  6. #6
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,615
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by sayeek View Post
    If you use my script then you got output like that

    Input = 152469854
    Output= 15,24,69,854

    Input= 12541362547589
    Output=1,25,41,36,25,47,589

    My script is simple and easy.
    Except, the money_format function can do that--and correctly (unlike your output example where you grouped numbers by twos instead of threes). It's rare that rewriting a built-in function is necessary. Although it might be a good exercise on occasion, the built-in functions are typically more efficient. They are there so you don't have to waste time building them and can get to building your site/app more quickly.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain


Tags for this Thread

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
  •