is_numeric does not work with decimal separator ,

Asked

Viewed 272 times

0

I’m using the function is_numeric in the case below does not work.
For example, if $val = 0,2 I want it changed to 0.2

Just to contextualize the other values could be:

  1. a string => in this case I want it to be just between single quotes: 'example'.
  2. empty => If empty I want empty single quotes to appear ''

Note: I have tried: if (is_numeric($val){}

foreach ($innerArray as $key=>$val) {

    if (is_numeric($val)){
        $val = str_replace(",", ".", $val);
        $aux .= $val . ",";
    } else {
        $aux .="'" . $val ."'" . ",";
    }
}
  • 0,2 is a string for having a comma. is_numeric won’t work.

  • So the question would be: How can I take the values with comma. Only the ones that are numbers and turn them to Number.

  • There are other comma values that are not treated as numbers?

  • There may be texts or anything else. In this application I’m developing I don’t know what kind of data comes in. Hence the problem. For example, if you arrive: [Hello, All right! ] => I just want it to be in simple quotes. If it comes empty [] =>I want to put simple quotes. If you arrive: [0,2] => I want to change to [0.2]

  • In PHP numerical data use the American standard, where the decimal separator is the point. You need to change the input of , for . before entering the is_numeric

  • I guess you’ll have to do it using regex.

  • I also think it will be around... I’m searching here how it would be with the preg_replace. If someone has a tip put there ;)

  • This numeric string is bounded by comma only?

  • Where does this value come from ?

  • 1

    Bruno, please edit the question to add details: click on [Edit] just below the tags and update your post. Thanks!

  • using regex ' d+, d+' does not address your problem? So, when the resulting array comes empty, it means that there are no numbers separated by comma, you just need to switch to the quotes.

  • This numeric string is bounded by comma only?

  • @brunoNascimento added an answer. See if it will help you.

  • 1

    is_numeric() is not a conversion method, is used to check if it is a number, it returns the boolean true if it is a number, or false if it’s not a number.

  • 1

    to convert to use number: intval($sua_string_numerica) for whole, and floatval($sua_string_numerica) to a float.

Show 10 more comments

3 answers

1

Joins the number_format() with the is_numeric - you can then use the number_format with arguments to make it the format you want. example

$number = '1,2';
echo is_numeric(number_format($number));


$number = ['1,2', 1, ''];
foreach ($number as $val) {
    if (!empty($val) || !ctype_alnum($val)) echo "val: $val is_numeric ".is_numeric(number_format($val))."\n\r";
}

You can also simply remove all letters and leave only the numbers before to do the foreach:

$number = 'abc1,2def';
$number = preg_replace(/[a-zA-Z]/i, '', $number);
echo 'is numeric? '.is_numeric(number_format($number));

1

Considerations

Well, first I will make some considerations before the validation logic implemented in your application, then I will focus on the resolution of your problem. You will decide which is more convenient.

Rules of entry.

Every application needs to have input rules. Such is the validation of user entries, everything it enters need to impose validation.

Taking into account that this value is a user input and your application may need to understand this entry as INT it will be necessary to validate, not perform calculation on the typed string.

I strongly recommend you validate this entry in the source and not turn the value to the correct output, so your application processing the user input value correctly.

Performance

Force the user to correctly type the value, for example:

float 0.2 rather than string 0,2 will improve the performance of your application.

Because then you won’t need to impose a transformation of Typecast convertion.

Saving application performance cost.

Manners

Security is the overriding factor, so let’s validate all user entries before processing them?

A simple if(is_numeric($string)) in the user input you can tell him if the value he is typing is valid for your application to process. So you enforce validation rules in your application.

Resolution

If your application comes to accept this type of input and you need to convert it to INT or FLOAT you can do it in the following ways:

First let’s take a replace in the string characters.

$string = str_replace(',', '.', $string);

Then just work with the floatval()

$float_value = floatval( $string );

Thus will convert the typecast string to int

Thus remaining:

$string = '0,2';

$string = str_replace(',', '.', $string);

$float_value = floatval( $string );

var_dump(floatval($float_value));  // 0.2

var_dump(is_numeric($float_value)); // true

This way it will be possible to process entries such as: 0,2, 0.2, 2, -9999 and 2a0 which will be understood as 2

1

To change your string to a value, just do this, which will already work:

<?php 
function convertNumber($val)
{
   return floatval(preg_replace('/([,])+/','.', $val));
}  

$val = '0,2';
echo convertNumber($val);  

?>

See here the working method:

http://ideone.com/X7nTyi

Obs: Use the method is_numeric() to check if it is a number and not to convert it. The expected output is a boolean for the method and not a converted number.

To apply to your loop, apply the method, but do a check if it contains a number in your string to apply the method:

$values = array();

foreach ($innerArray as $key=>$val) {
     if(is_numeric($val) || preg_match('/[0-9]+/',$val)) {
        $values[] = convertNumber($val);
     } else {
        $values[] = "'{$val}'";
     }
}

$saida = implode(', ', $values);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.