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.
Te ha servido? Valora esta entrada!
(1 voto, promedio: 5 de 5)
Comparte en las redes sociales
¿Buscas trabajo de programador?

8 respuestas a “Redondear con precisión con PHP”

  1. katalog polskich firm dice:

    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.

  2. poussette bébé pas cher dice:

    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.

  3. climatiseur mobile silencieux dice:

    I’d constantly want to be update on new blog posts on this site, saved to my bookmarks ! .

  4. music to download dice:

    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.

  5. chaussure de foot dice:

    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!

  6. Popularne torrenty dice:

    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!

  7. agen sbobet dice:

    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.

  8. Jamie Kothe dice:

    I am impressed with this site, really I am a big fan .

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.

Ver más sobre