Belajar Tidak Identik dengan Buku

PHP, Rounding and Formatting Numbers

1. Round () – Rounding Numbers

The PHP function round () is used to round numbers up or down based on their value (Any decimal .5 or above is rounded up, anything under .5 is rounded down.) You can specify how many decimals you would like the number rounded to. It is phrased as round ( number, optional_decimals ) If left unspecified, by default it will round to the nearest whole number. You can use negative numbers when specifying the optional_decimals, for example -1 would round to the nearest 10, -2 would round to the nearest 100, etc.

example :

<php
echo round(3.14159265); // Value would be 3
echo round(3.9); // Value would be 4
echo round(3.14159265, 3); // Value would be 3.142
echo round(314159, -3); // Value would be 314000
?>

2. Ceil () – Always Rounding Up

The Ceil () PHP function is used to always round up to the nearest whole number. The PHP function Ceil () behaves identical to the MySQL use; even the smallest decimal will always be rounded up.

example :

<?php
echo ceil(3.14159265); // Value would be 4
echo ceil(3.00000001); // Value would be 4
echo ceil(-3.14159265); // Value would be -3
?>

3. Floor () – Always Rounding Down

The Floor() PHP function is used to always round down to the nearest whole number. The PHP function Floor () behaves identical to the MySQL use; even the largest decimal will always be rounded down.

example :

<?php
echo floor(3.14159265); // Value would be 3
echo floor(3.99999999); // Value would be 3
echo floor(-3.14159265); // Value would be -4
?>

4. Number_format () – Formatting your Numbers

You can use the PHP function number_format to format a number in several ways, including choosing how many decimal points it will have, and choosing the 1000s, and decimal point dividers. It is phrased as number_format ( your_number , optional_decimals , optional_decimal_point, optional_1000_separator ) You can not specify just the decimal point or 1000 separator, if you specify one you need to specify both. When working with decimals, it will function like round () and put things at or over .5 up, and under .5 down.

example :

<?php
echo number_format (1234.567); //Returns the number 1,235
echo number_format (1234.567, 2); //Returns the number 1,234.57
echo number_format (1234.567, 2, ',', ' '); //Returns the number 1 234,57
echo number_format (1234.567, 1, 'a', 'b'); //Returns the number 1b234a6
?>

Key : PHP, Rounding and Formatting Numbers

Related Posts

Rounding and Formatting Numbers

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Happy Paperless located at Banjarnegara , Jawa Tengah, Indonesia . Reviewed by 28 customers rated: 1 / 5



Rumus-Excel.Com
Kunjungi rumus-excel.com untuk belajar excel. Anda juga dapat menyumbangkan artikelnya ke kami untuk kita publish