#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    73
    Rep Power
    0

    Eliminate thousand dot for thousands only


    Hi,
    I have a database mysql with decimal (10.2) columns,
    so I need the price to insert like this 2456.75

    Often amounts copied from banks are inserted, and the banks displays like this 2.456,75, and of course a copy and paste is imposible as if I try to insert the amount displayed in bank the amount of 2.45 will be inserted.
    To change the coma for a dot I use this:
    $cantidad = str_replace(",", ".", $cantidad);
    However the dot for the thousands must be taken away before inserting,
    I cant do the same for the dot (replace '.' with '') as when we insert manully without copying and pasting we do it correctly writing 2456.75 to insert.

    Anyway of deleting the dot but only for thousands?
  2. #2
  3. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    501
    Rep Power
    127
    There might be a prettier way, but the logic below seems to work.. and demonstrates it:

    PHP Code:
    <?php
        
    foreach(
            array(
                
    '45,213.43',
                
    '6,445,213.43',
                
    '6,445,213',
                
    '45.213,43',
                
    '6.445.213,43',
                
    '6.445.213',
            ) as 
    $number
        
    ){
            print 
    "$number<br/>\n";
            
    $number preg_replace('/[.,](?![\w]{0,2}$)/','',$number);
            print 
    str_replace(',','.',$number)."<br/><br/>\n";
        }
    This also allows for the user to exclude the cents.

    Comments on this post

    • newtonperri agrees : good solution
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    73
    Rep Power
    0
    Originally Posted by dmittner
    There might be a prettier way, but the logic below seems to work.. and demonstrates it:

    PHP Code:
    <?php
        
    foreach(
            array(
                
    '45,213.43',
                
    '6,445,213.43',
                
    '6,445,213',
                
    '45.213,43',
                
    '6.445.213,43',
                
    '6.445.213',
            ) as 
    $number
        
    ){
            print 
    "$number<br/>\n";
            
    $number preg_replace('/[.,](?![\w]{0,2}$)/','',$number);
            print 
    str_replace(',','.',$number)."<br/><br/>\n";
        }
    This also allows for the user to exclude the cents.
    That looks like it works, thousands of thanks

IMN logo majestic logo threadwatch logo seochat tools logo