Value rounding up php does not work

Asked

Viewed 199 times

-2

I want to transform the value returned from a sum, formatted without point(.) or comma characters(,)

$valor comes from a POST, the following sum is made:

echo $porcent * ceil($valor);

The above code returns me the following value 99.99 or higher 999.99 I believe, hence I want this value to return 9999 no semicolon. I’m tapping the coconut and I get no result.


Follows the code.

<?php $valor = $_POST['valor']; $porcent = $_POST['porcent'] / 100; ?>
</head>
<body>
    <span style="width:40px" class="text-muted"><?php echo $porcent * ceil($valor); ?></span>
  • Give numerical example, who is $percent and who is $value

  • If you are multiplying an integer given by the Ceil function by a noninteger value it is evident that this can result in a noninteger number. do echo ceil($porcent * $valor);

  • What values are you going through the POST for valor and porcent?

1 answer

1


First you should do the calculation and then round off the value:

<?php
   $valor = 1863;
   $porcent = 20 / 100;
   $resultado = $porcent * $valor;
?>
<span style="width:40px" class="text-muted"><?php echo ceil($resultado);?></span>

functional example in ideone

  • It worked as expected, but returned the following news warning: Notice: A non well Formed Numeric value encountered in can help me?

  • You can in this alg instead of this round up value, it just remove the type formatting: 34.87 to 3487

  • you commented this error, but I did the tests here and nothing occurred and also the link that the friend above posted is working properly

  • you can use this function: number_format($result, 2, '', ''), where the variable or value goes, then the number of boxes after the comma, then the decimal separation type and the thousand separation type (as you can see are simple quotes, with nothing, so that everything stay together, if you want to separate by a few things, just put something between the quotes).

  • I already found the error, I was emitting the value in the input field with comma(,) and so emitted the result but warning me this... You know if there’s any way I can make it so that even by typing the comma value it adds up?

  • but, what sum?

Show 1 more comment

Browser other questions tagged

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