You are here: Re: number formats and posted data « PHP Language « IT news, forums, messages
Re: number formats and posted data

Posted by Ric on 12/23/06 00:04

nostabo schrieb:
> Thanks, Lost...
>
> the str_replace(',', '', $_POST['numbers']) works. I was hoping for
> something more elegant, possibly an "undocumented feature" of the
> number_format function, but this works fine.

Hmm, but if you do that:

1,35 will show up as 135, not sure if that is what you want:-)

If you want to make sure a user can type any possible combination you
need a few regexes to first determine which format the user send and
then convert it to the format you need for calculation.


>
> Rick
>
> "-Lost" <spam_ninjaREMOVEME@REMOVEMEcomcast.net> wrote in message
> news:Ho-dnc-kDbBXwBHYnZ2dnUVZ_t6qnZ2d@comcast.com...
>> "nostabo" <nostabo@microsoft.com> wrote in message
>> news:aOmdnbRucLWo2hHYnZ2dnUVZ_h-3nZ2d@comcast.com...
>>> Hi all,
>>>
>>> I have been trying to display numbers (actually money amounts) on an
> HTML
>>> form in text type input boxes and then use the posted values in
> calculations
>>> using PHP. The problem is that I can accept inputted values from a text
> box
>>> when entered in the 1000 or 1000.00, but when a users adds a comma the
> value
>>> (for example 1,000.00) the value is displayed as 1.0 by the
> number_format
>>> function (see my code below)
>>>
>>> <input type='text' id='software' name='software' value='<?php print
>>> number_format($_POST['software'], 2);?>' >
>>>
>>> But, this also affects any reposting of the form since the posted var
> takes
>>> on the value as displayed in the text box.
>>>
>>> Is there a way around this? This bad value eventually shows up in the
>>> "posted" var if the form is resent after the user makes a change, so it
> can
>>> screw up my calculations.
>> $_POST['numbers'] = '1,000,000';
>> print str_replace(',', '', $_POST['numbers'])
>>
>> Would be my first guess. Some might say use a regular expression here,
> but it is faster
>> to use native functions (functions that serve the *exact* purpose).
>>
>> -Lost
>>
>>
>
>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация