For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > December 2006 > number formats and posted data









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author number formats and posted data
nostabo

2006-12-22, 6:58 pm

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.

TIA,

Rick


-Lost

2006-12-22, 6:58 pm

"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


Ric

2006-12-22, 6:58 pm

nostabo schrieb:
> 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);?>' >


this shoudl do it:

number_format($_POST['software'], 3)


>
> 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.
>
> TIA,
>
> Rick
>
>

nostabo

2006-12-22, 6:58 pm

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.

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...
HTML[color=darkred]
calculations[color=darkred]
box[color=darkred]
value[color=darkred]
number_format[color=darkred]
takes[color=darkred]
can[color=darkred]
>
> $_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
>
>



nostabo

2006-12-22, 6:58 pm

Thank you, but this simply adds another decimal place to the truncated
value.
1.000 instead of 1.00

Rick

"Ric" <antispam@randometry.com> wrote in message
news:emhntf$dk$1@online.de...[color=darkred]
> nostabo schrieb:
HTML[color=darkred]
calculations[color=darkred]
box[color=darkred]
value[color=darkred]
number_format[color=darkred]
>
> this shoudl do it:
>
> number_format($_POST['software'], 3)
>
>
takes[color=darkred]
can[color=darkred]


Ric

2006-12-22, 6:58 pm

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...
> HTML
> calculations
> box
> value
> number_format
> takes
> can
> but it is faster
>
>

-Lost

2006-12-22, 9:58 pm

> "Ric" <antispam@randometry.com> wrote in message
> news:emhntf$dk$1@online.de...
> HTML
> calculations
> box
> value
> number_format

"nostabo" <nostabo@microsoft.com> wrote in message
news:x9Gdnd76rqcZ8hHYnZ2dnUVZ_s-rnZ2d@comcast.com...[color=darkred]
> Thank you, but this simply adds another decimal place to the truncated
> value.
> 1.000 instead of 1.00
>
> Rick


Yeah, I was not quite sure of that either. It truncates any number to its first number
and three decimal 0s.

1,000,000 becomes 1.000

123,456 becomes 1.000

645,321 becomes 6.000

I guess it is not worse than stripping the commas and replacing them with pipes (|),
something I recently had to do in this wonky financial application. Nothing like seeing:

123|456|789:123

....to make you sick of numbers quick.

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


French decimals... ugh. : P

-Lost


Jussist

2006-12-23, 7:58 am

It seems that you very well know what are all the possible cases. Then
just write down all the cases like from => to:

1,000 => 1000
1.000 => 1000
1,35 => 1.35
etc...

And figure out logic needed for each case, and figure out common logic
that does the trick for all cases.

Gleep

2006-12-23, 6:57 pm

On Fri, 22 Dec 2006 13:00:03 -0800, "nostabo" <nostabo@microsoft.com> wrote:

>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.
>
>TIA,
>
>Rick
>



What you want to do here is separate out formatting from the content, run or save your calculation
then display it back with formatting.

function clear_currency($cca){
$ccb = array("$","," );
$ccc = str_replace($ccb, "",$cca );
return $ccc;
}

you get the submitted value of the form

$someAmount = clear_currency( $_POST['dollarAmount'] );

Note depending on your app you could also have some thing that removes the cents .00
by using substring command

list($dollars, $change) = split('.', $amount);

also sometimes people decide to be wise asses and type in crazy stuff when you are expecting a
number only that you could add this function...

function numberOnly($no){
$nno = ereg_replace ('[^0-9]+', '', $no);
return $nno;
}


So now you know how to filter out a currency to a regular number, now run your calc, then you need
to display it back you could use this

<?= "\$".number_format($number, 2, '.', '') ?>






-Lost

2006-12-23, 6:57 pm

"Gleep" <Gleep@Gleep.com> wrote in message
news:2s1ro255i7dqsdniqt7qicbj3c3mr3kjhd@
4ax.com...
> On Fri, 22 Dec 2006 13:00:03 -0800, "nostabo" <nostabo@microsoft.com> wrote:
>
>
>
> What you want to do here is separate out formatting from the content, run or save your
> calculation
> then display it back with formatting.
>
> function clear_currency($cca){
> $ccb = array("$","," );
> $ccc = str_replace($ccb, "",$cca );
> return $ccc;
> }
>
> you get the submitted value of the form
>
> $someAmount = clear_currency( $_POST['dollarAmount'] );
>
> Note depending on your app you could also have some thing that removes the cents .00
> by using substring command
>
> list($dollars, $change) = split('.', $amount);
>
> also sometimes people decide to be wise asses and type in crazy stuff when you are
> expecting a
> number only that you could add this function...
>
> function numberOnly($no){
> $nno = ereg_replace ('[^0-9]+', '', $no);
> return $nno;
> }
>
>
> So now you know how to filter out a currency to a regular number, now run your calc,
> then you need
> to display it back you could use this
>
> <?= "\$".number_format($number, 2, '.', '') ?>


Great examples, Gleep!

I would however test my cleaned inputs with native functions though. For example,
replacing your numberOnly() function with ctype_digit(). Of course there is ctype_alnum()
for alphanumeric and ctype_alpha() for all alphabetical (for those who were not aware).

Be well and Happy Holidays.

-Lost


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com