Page 2 of 2 First 12
  • Jump to page:
    #16
  1. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,118
    Rep Power
    1990
    What you need to do is store the value in the database in a DECIMAL column. When a user enters the number, check it with is_numeric() to see if it's a numeric value or not. You can even use floatval() to convert the value to a number. When you display it, use number_format() and it will display the number just how you want it to.
  2. #17
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    78
    Rep Power
    0
    Originally Posted by drumn4life0789
    I guess I don't understand what is really going on here.

    Seems to me that all that needs to be done is strip out anything that is not a number or a decimal. Then check to make sure that there is only one decimal in the string. Then you check that if there is a decimal that there is only two digits behind it.

    if we get all that as true we have a valid input. But since it is money I would say you throw it back to the user and ask if this is correct. And format it like so in the display, so that it is easy for the user to read
    $###,###.##
    Yes, but cant find how to do.
    This does it but leaves the dot also for thousand, this gives me 1.100.50 when I need 1000.50
    $price = '1.010.10';
    $price = preg_replace("/[^0-9\.]/", "",$price);
    and the filters, think arent using ok, maybe this is what I need however I doesnīt do it correctly:
    $var = filter_var($price,FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
    echo $var;
    Thanks
  4. #18
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    78
    Rep Power
    0
    Originally Posted by Catacaustic
    What you need to do is store the value in the database in a DECIMAL column. When a user enters the number, check it with is_numeric() to see if it's a numeric value or not. You can even use floatval() to convert the value to a number. When you display it, use number_format() and it will display the number just how you want it to.
    I am trying, but this and everything I do for 1.010.00 returns 1.01
    as 1.010 is one number as gW said, so as there are 2 numbers in 1.010.00 what should I do, split them, delete the dot.
    I am lost, as they may fill in the form many ways 1.000.10 and 1000.10 should end both 1000.10 and 1000 should end 1000.00

    The only dot acceptable is a dot for decimals, and if none a .00 should be added.
  6. #19
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,496
    Rep Power
    594
    I am not a regex expert (ask on the regex forum) but I'm sure there is an expression that can strip out all non-numerics except the last decimal, if it exists.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #20
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    78
    Rep Power
    0
    Originally Posted by gw1500se
    I am not a regex expert (ask on the regex forum) but I'm sure there is an expression that can strip out all non-numerics except the last decimal, if it exists.
    thanks will do that, didnīt know there was a special forum for that.
  10. #21
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    78
    Rep Power
    0
    Doing this I manage to convert the empty value for field decimal to 0, filling in the form field price 2 and field decimal leave it emtpy the echo echo $holidaycost; before the ctype_digit check prints correctly 2.0 but after the ctype_digit check it prints the value empty....
    I need to validate the field decimal.
    PHP Code:
          $price htmlspecialchars($_POST['price']);
           
    $decimal htmlspecialchars($_POST['decimal']);;
    if (empty(
    $decimal)) {
    $decimal ='0';
     
    $holidaycost $price.".".$decimal;
     echo 
    $holidaycost;
    echo 
    "<br>";
     }
     if ( 
    ctype_digit($_POST['price'])&& ctype_digit($_POST['decimal'])) {
     
    $holidaycost $price.".".$decimal;
     echo 
    "test $holidaycost";    
     } 
  12. #22
  13. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,496
    Rep Power
    594
    PHP Code:
    $holidaycost $_POST['price'].".".$_POST['decimal']; 
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  14. #23
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    78
    Rep Power
    0
    Originally Posted by gw1500se
    PHP Code:
    $holidaycost $_POST['price'].".".$_POST['decimal']; 
    Thanks, it didnīt work, but this did:
    PHP Code:
     if ( ctype_digit($_POST['price'])&& ctype_digit($decimal)) { 
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo