PHP, as I do for when the number after. for 0 to be hidden?

1

Good Night

I have a sample value of 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 dot get hidden sample 100.00 show 100 and if it is 100.30, show normal for user.

I thought about using IF and number_format , but I'm not sure if that mode is the right one or there is an easier way.

Thank you

    
asked by anonymous 26.04.2017 / 02:37

4 answers

4
  

100.30 +0 returns 100.3 and I think what you want is "if it is 100.30, show normal for 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>
    
26.04.2017 / 04:47
3

Formatting with type float

When the variable does not have the value inside quotes, the ZEROs to the right are omitted "automatically"

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

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

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

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

If this is not possible, a cast is required.

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 "cute" or "shorter" option does not mean that it is faster.

Formatting string type

If you still want to display the zero to the right of a fractional value, one option is to do a formatting that identifies that 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;

More briefly, you can do just that

$n = str_replace('.00', '', number_format($n, 2, '.', ''));
echo $n;
    
26.04.2017 / 08:06
1

You can use str_replace , like this:

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

This will remove .00 if there is, if no .00 is found 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

Try this here.

    
26.04.2017 / 20:48
0

The simplest way I see is to verify that the value is numerically equal to type cast for int of it, since "123.00" == (int) "123.00" returns true , while "123.10" == (int) "123.10" returns false . 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

Running on Ideone .

    
26.04.2017 / 21:18