PHP, how do I stop when the number after . is 0 is hidden?

Asked

Viewed 2,070 times

1

Goodnight

I have a value example 100.00 and 100.30, in my table in my BD, as I do to give an echo when the numbers are 00 after the point get hidden example 100.00 show 100 and if it is 100.30, show normal for user.

I thought I’d use IF and the number_format, but I’m not sure if this way more correct or there is an easier way.

Thank you

  • You already have an answer to this question at stackoverflow. http://stackoverflow.com/q/14531679/2370479

  • Exactly. Take a look at the floatval function()

  • Good night is right, thank you

4 answers

4


100.30 +0 returns 100.3 and I think what you want is "if it is 100.30, show normal to user".

PHP

    $acheme=".00";
    $meunumero=$_POST['numero'];

    $pos = strpos($meunumero, $acheme);

    if ($pos === false) {
        echo $meunumero;
    } else {
        echo substr($meunumero,0,-3);
        //ou se preferir use
        //echo $meunumero+0;
    }

HTML

<form name='form' method=post action='' >
    <input type="text" name="numero" class="nome1" value="" />
    <input type="submit" name="testar">
</form>

3

Formatting with float type

When the variable does not have the value inside quotes, the Zeros on the right are omitted "automatically"

$n = 100.00;
echo $n; // retorna 100

$n = 100.30;
echo $n; // retorna 100.3

When it is delimited by single quotes or double quotes, it is treated as a string at the time of printing. The above examples would return 100.00 and 100.30, respectively.

Therefore, if it is not important to display zero when there is a fractional value, 100.30, just do not delineate with quotation marks.

If not possible, you need to cast.

There are various forms that return the same result:

$n = '100.30';
echo (float)$n; // retorna 100.3

$n = '100.30';
echo floatval($n); // retorna 100.3

$n = '100.30';
echo $n + 0; // retorna 100.3

$n = '100.30';
echo +$n; // retorna 100.3

*The more "cute" or "short" option doesn’t mean it’s faster.

Formatting the string type

If you still want to display zero to the right of a fractional value, one option is to make a formatting that identifies such a condition.

// O valor pode ser número ou string
//$n = '100.00';
//$n = 100.00;
//$n = '100.30';
$n = 100.30;

// Garante que a formatação mantenha 2 casas decimais
$n = number_format($n, 2, '.', '');

// Abstrai a parte decimal
$d = substr($n, -2);

// Se for 00, então, faz o cast para removê-los
if ($d === '00') {
    $n = (float)$n;
}

// Imprime o resultado
echo $n;

In a more succinct way, you can do just that

$n = str_replace('.00', '', number_format($n, 2, '.', ''));
echo $n;

1

You can use the str_replace, thus:

str_replace('.00', '', $numero);

This will remove the .00 if there is, if the .00 no change will be made.

Entrada:   100.00
Resultado: 100

Entrada:   100.30
Resultado: 100.30

Entrada:   1
Resultado: 1

Entrada:   1.01
Resultado: 1.01

Test it out here.

0

The simplest way I see is to check if the value is numerically equal to type cast for int his, for "123.00" == (int) "123.00" altarpiece true, while "123.10" == (int) "123.10" altarpiece false. In this way:

$number = "123.00";
echo ($number == (int) $number) ? (int) $number : $number, PHP_EOL;
// 123

$number = "123.10";
echo ($number == (int) $number) ? (int) $number : $number, PHP_EOL;
// 123.10

$number = "123.1000";
echo ($number == (int) $number) ? (int) $number : $number, PHP_EOL;
// 123.1000

Functioning in the Ideone.

Browser other questions tagged

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