Redondear con precisión con PHP
PHP dispone de una función para hacer redondeos con precisión. Se trata de la función round() que tiene la siguiente estructura:
float round ( float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]] )
Esta función la podemos utilizar en casos como los siguientes:
<?php echo round(3.4); // 3 echo round(3.5); // 4 echo round(3.6); // 4 echo round(3.6, 0); // 4 echo round(1.95583, 2); // 1.96 echo round(1241757, -3); // 1242000 echo round(5.045, 2); // 5.05 echo round(5.055, 2); // 5.06 ?>
O también podríamos utilizar sus constantes:
<?php echo round(9.5, 0, PHP_ROUND_HALF_UP); // 10 echo round(9.5, 0, PHP_ROUND_HALF_DOWN); // 9 echo round(9.5, 0, PHP_ROUND_HALF_EVEN); // 10 echo round(9.5, 0, PHP_ROUND_HALF_ODD); // 9 echo round(8.5, 0, PHP_ROUND_HALF_UP); // 9 echo round(8.5, 0, PHP_ROUND_HALF_DOWN); // 8 echo round(8.5, 0, PHP_ROUND_HALF_EVEN); // 8 echo round(8.5, 0, PHP_ROUND_HALF_ODD); // 9 ?>
Autor
Escrito por Jose Aguilar - Director ejecutivo y tecnológico en JA Modules. Experto programador PrestaShop y Experto programador WordPress.
You can definitely see your expertise in the work you write. The sector hopes for more passionate writers like you who are not afraid to say how they believe. At all times go after your heart.
Keep up the great work, I read few posts on this internet site and I think that your weblog is real interesting and holds lots of wonderful info.
I’d constantly want to be update on new blog posts on this site, saved to my bookmarks ! .
I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business.
you are in point of fact a just right webmaster. The website loading pace is amazing. It seems that you are doing any distinctive trick. Also, The contents are masterpiece. you have performed a magnificent process in this matter!
Wow that was strange. I just wrote an very long comment but after I clicked submit my comment didn’t appear. Grrrr… well I’m not writing all that over again. Anyhow, just wanted to say wonderful blog!
Wow! This can be one particular of the most beneficial blogs We’ve ever arrive across on this subject. Basically Wonderful. I am also a specialist in this topic therefore I can understand your hard work.
I am impressed with this site, really I am a big fan .