Re: number_format problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: "Dillon, John" <JDillon@xxxxxxxxxxxx>

> I want to show a number from a database in the format x,xxx.00 in a
> textfield, then if it is changed by the user I want to post the value of
the
> number to a decimal field.  However, once you number_format the number it
> becomes a string, and numbers like 3,379.90 give a value of 3 when posted
to
> the database, which is hinted at in the notes on number_format.  I suppose
I
> need a string to number function - can someone tell me what this might be
> called please?

If you know it's going to be in a $x,xxx.xx format, then

$new_number = preg_replace('/[^0-9.]/','',$old_number);

will remove anything that's not a number or decimal point.

BUT, since we know that we can't trust it'll come in that format, you may
also want to run

$rs = preg_match('/[0-9]+\.?([0-9]{1,2})?/',$new_number); //untested :)

which _should_ make sure you're not getting a number like "xxx.xx.xxx" or
"xx.xxxxxx" from some tricky user. If $rs is TRUE or 1, then the number is
in the correct format.

---John Holmes...

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux