user485783
user485783

Reputation: 1685

read the currency format

perhaps for some people this is easy, but I want to learn

There are 2 currency formats:

first currency format is 1,123,123.12 and this format may be like $1,123,123.12 or 1,123,123.12€ and

second currency format is 1.123.123,12 and this may be such $1.123.123,12 or 1.123.123,12€ so the difference is the placement of dots and commas

The above format will be

$this->value('one of the currency format insert here');

e.g. $this->value('$1,123,123.12'); or $this->value('1.123.123,12€');

that I want to know is the code if (first currency format) {use blah .. blah ..} elseif (second currency format) {use blah .. blah ..} else {/ / unsupported format}

so how the code to identify whether the entry is input the first currency format or second currency format?

thanks for the your pointers and ideas.

UPDATED:

I apologize for mistake in giving examples

When I tried to test the code I have a little confused because it seems not working

then I changed my prevoious parts, so that $value['amount'] it may be use

first currency format 1,123,123.12 and this format may be like $1,123,123.12 or 1,123,123.12€ and

second currency format 1.123.123,12 and this may be such $1.123.123,12 or 1.123.123,12€

then the value['amount'] will identify first with code like the following conditional

class curr_format {

private bla...bla..1
private bla...bla..2
var etc..

    public function curr_format ($bla...,$and_bla..) {

//then make conditional is here
if (first currency format) {//use blah .. blah ..} 
elseif (second currency format) {//use blah .. blah ..} 
else {/ / unsupported format}
//another codes..

at the end output as look like:

$identify = new curr_format();
echo $identify->curr_format($value['amount'],$else_statement);

Upvotes: 1

Views: 442

Answers (3)

Luke Stevenson
Luke Stevenson

Reputation: 10341

function getNumber( $inStr ){
  if( preg_match( '/[\,\.]\d{2}\D?$/' , $inStr ) ){
   # Has 2 Decimal Places
    return (float) preg_replace( '/\D/' , '' , $inStr )/100;
  }
  return (int) preg_replace( '/\D/' , '' , $inStr );
}

In cases where there are 2 decimals, we strip all non-digit characters, and then divide by 100 (thereby replacing the decimals). If there are not 2 decimals, we just strip all non-digit characters.

Upvotes: 1

Nathan
Nathan

Reputation: 11149

Assuming you're only working with dollars and euros (or pounds or anything else that has two decimal digits, the easiest solution is:

var value = str_replace('€','',$this->value);

if (substr($value,str_len($value)-3,1)=='.') {
  // first format
}
else if (substr($value,str_len($value)-3,1)==',') {
  // second format
}
else {
  // unsupported
}

Otherwise, you would have to set up a loop which iterates back through the characters one by one, or determines the number of digits back based on the currency.

Upvotes: 1

Jason
Jason

Reputation: 15358

Check what the 3 last character is, if it is a "," or a ".". Use this to determine your currency format.

$value =  "1,123,123.12";
if($value[strlen($value)-3]==',') $currency_format = "A";
else $currency_format="B";

Upvotes: 2

Related Questions