clean currency input

This is a discussion on clean currency input within the PHP Language forums, part of the PHP Programming Forums category; Hi Folk I wrote the following function as a "cleaner" for a currency input: function is_currency($v) { //returns ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-06-2006
windandwaves
 
Posts: n/a
Default clean currency input

Hi Folk

I wrote the following function as a "cleaner" for a currency input:


function is_currency($v) {
//returns a string with only the numbers for a currency format
$v = str_replace("$", "", $v);
$regex = '/(?:([0-9]+)|.)/';
if(preg_match($regex, trim($v) ) ){
$v = $v + 0.00001;
if (is_float($v)) {
return round($v, 2);
}
}
else {
return 0;
}
}

I want users to be able to put in things like:
NZ$12.34
$12.34
12
12.3456
1,002.23
and it should always translate into a number with two decimals (e.g. 12.34
or 12.00)

I am sure that this is probably the clumsiest way ever to do this. How can
I improve this function?

TIA

Nicolaas
PS despite numerous efforts I am still super weak on regular expressions. I
can only copy them and not write them myself.


Reply With Quote
  #2 (permalink)  
Old 01-06-2006
Dmitry Ruban
 
Posts: n/a
Default Re: clean currency input

Hello Nicolaas,

Take a look at my code for this action, i think it's more accurate:

function is_currency( $v )
{
$v = preg_replace("/[^0-9.]+/","",$v);
return round($v,2);
}


"windandwaves" <winandwaves@coldmail.com> сообщил/сообщила в новостях
следующее: news:cIovf.12929$vH5.660283@news.xtra.co.nz...
> Hi Folk
>
> I wrote the following function as a "cleaner" for a currency input:
>
>
> function is_currency($v) {
> //returns a string with only the numbers for a currency format
> $v = str_replace("$", "", $v);
> $regex = '/(?:([0-9]+)|.)/';
> if(preg_match($regex, trim($v) ) ){
> $v = $v + 0.00001;
> if (is_float($v)) {
> return round($v, 2);
> }
> }
> else {
> return 0;
> }
> }
>
> I want users to be able to put in things like:
> NZ$12.34
> $12.34
> 12
> 12.3456
> 1,002.23
> and it should always translate into a number with two decimals (e.g. 12.34
> or 12.00)
>
> I am sure that this is probably the clumsiest way ever to do this. How

can
> I improve this function?
>
> TIA
>
> Nicolaas
> PS despite numerous efforts I am still super weak on regular expressions.

I
> can only copy them and not write them myself.
>
>



Reply With Quote
  #3 (permalink)  
Old 01-06-2006
Janwillem Borleffs
 
Posts: n/a
Default Re: clean currency input

windandwaves wrote:
> I am sure that this is probably the clumsiest way ever to do this. How can
> I improve this function?
>


function format_currency($n) {
$n = preg_replace('/[^\d.]+/', '', $n);
return sprintf('%01.2f', $n);
}

As you will notice, I have also modified the function name as "is_currency"
suggests that it returns a boolean rather then a formatted number.

For currency formatting you can apply the number_format() function, in case
you've missed it.


JW


Reply With Quote
  #4 (permalink)  
Old 01-07-2006
windandwaves
 
Posts: n/a
Default Re: clean currency input

windandwaves wrote:

Thanks guys -

That is awesome.

Nicolaas


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 11:55 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0