You are here: Re: clean currency input « PHP Programming Language « IT news, forums, messages
Re: clean currency input

Posted by Dmitry Ruban on 10/06/25 11:36

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.
>
>

 

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

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация