How do I concatenate a decimal value?

Asked

Viewed 223 times

-1

Well I have the following code:

<?php
    $numero = "5";
    $decimal = "30";   
?>

What I want to do is concatenate the number so that the final result is 5.3 and so that the right zeros do not contain...

How can I do that?

2 answers

7


What you can do is transform $numero and $decimal in a value accepted by the function floatval().

For that just concatenate $numero, a point and $decimal.

Example:

<?php
$numero = "5";
$decimal = "30";
echo floatval("$numero.$decimal");  // floatval("5.30")

Remembering that my solution hopes that $numero and $decimal contains only numeric characters.

5

What you want is not to concatenate, it is the opposite, it is to transform texts into numbers, with a specific criterion and make the sum of those numbers. The most common is to use one cast to make this conversion, but nothing guarantees that it will work and you may have problems.

Note that creating the decimal part of the number is your problem, only your code can indicate that this is what you want, so I divided by 100.0 (you can’t just divide by 100 because there is an integer value). I preferred to convert to int because there are indications that the number to be used can no longer be decimal. If you have the concatenation done before and then convert an additional level of possibility of error.

Unbelievably PHP doesn’t have a ready-made function that generates error if the conversion fails (except I’m well out of date in PHP), so to make sure it works you’ll have to make a parser in the hand. Without doing this you cannot trust the value, because it will generate the value 0 without indicating anything that gave error. I imagine you can’t trust the number, so you have to validate it. If you can trust me, I don’t know if you need to do what you want to do. I put in the example above how the error can pass beaten.

I’m not sure if it’s reliable, but the function is_numeric() may be the solution.

$numero = "5";
$decimal = "30";
echo (int)$numero + ((int)$decimal / 100.0);
echo "\n";
if (is_numeric($numero) && is_numeric($decimal)) echo (int)$numero + ((int)$decimal / 100.0);
else echo "melhor não calcular, tem erro aí";
$decimal = "a30";
echo "\n";
echo (int)$numero + ((int)$decimal / 100.0);
echo "\n";
if (is_numeric($numero) && is_numeric($decimal)) (int)$numero + ((int)$decimal / 100.0);
else echo "melhor não calcular, tem erro aí";

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

The parentheses in the second expression are more to make clear, they are not necessary. If you prefer you can use the function intval().

I did not enter into the merit that this can be a monetary value and this can be another problem. More out of curiosity if it is not that read What is the correct way to use the float, double and decimal types?

  • This "silently fail" part of PHP annoys me, I think it’s a design flaw. python makes an exception TypeError for any unsuccessful conversion.

  • 1

    Just for the record, is_numeric is not so reliable. She accepts "+0123.45e6" as valid, because it is a string that represents a float but not for this specific case. I think in this case it would be better to check $numero and $decimal with ctype_digit or a regex itself.

Browser other questions tagged

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