Decimals handling in PHP


PHP Round Off Decimal or Convert to Decimal Point

Round Off Decimal

Round a number up or down or Round off a floating decimal point number using PHP’s functions round().

Example. round();

 

 

<?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
?>

Convert to Decimal Point

Example. number_format();


<?php
$number = “28”;
$number = number_format($number, 2);
echo $number; // equals “28.00”
?>
 
 

 

PHP number formatting for decimal places

PHP has powerful math functions.
Formatting a number is required while displaying or preparing for some other processing.
One common requirement is displaying values after a division with fractions.
One way is to round off the number or we can format the number with specifying whether to display the digits with decimal or without decimal or number of digits to display after the decimal.
We can place comma ( ,) after a group of thousands. We can use other symbols in place of comma (,) for thousands and we also can replace (.) dot with symbol of our choice to be used as decimal.
Using PHP number_format function we can do all these formatting. Here are some examples for number formatting.

$number=2512589.66785;
echo “Number = $number “;
echo ” The value after formating = “.number_format($number);

// The above line will display 2,512,590 ( without decimal and rounded)

echo “<br> The value after formating = “.number_format($number,3);
// Will display 2,512,589.668 ( with , after each thousand and 3 digits after decimal
// rounded )

echo “<br> The value after formating = “.number_format($number,3,”#”,”:”);
// Will display 2:512:589#668 ( with : after each thousand and # as decimal symbol )

Happy Coding 🙂

 

Advertisements

Javascript Decimals and Significant digits


One of the most common tasks is to format a number for currency display- an integer followed by two decimals.
You may be tempted to use number rounding to first shift the number’s decimal places (via multiplication), round it, then shift the decimal back (via division) to pound the number into your hard earned dollar, though that won’t work in many cases.
For example, consider the number 120. Number rounding certainly won’t get you to 120.00.

To easily format numbers for a specific number of trailing decimals or total digits (aka padding), JavaScript 1.5 introduces the below two methods:
 

Methods                      Description                                                                            

Number.toFixed(x)              Formats any number for “x” number of trailing decimals.
                                           The number is rounded up, and “0”s are used after the decimal point
                                           if needed to create the desired decimal length.

Number.toPrecision(x)        Formats any number so it is of “x” length. Also called significant digits.
                                           A decimal point and “0”s are used if needed to create the desired length

Number.toFixed()

The best way to see all the subtleties of toFixed() is to see it in action:

var dummyNumber=2489.8237
dummyNumber.toFixed(3) //returns 2489.824 (round up)
dummyNumber.toFixed(2) //returns 2489.82
dummyNumber.toFixed(7) //returns 2489.8237000 (padding)

Displaying any number in currency format can’t get any easier!

Number.toPrecision()

To toPrecision() now:

var dummyNumber=123.45
dummyNumber.toPrecision(6) //returns 123.450 (padding)
dummyNumber.toPrecision(4) //returns 123.5 (round up)
dummyNumber.toPrecision(2) //returns 1.2e+2 (you figure it out!)

toPrecision() is useful if your number must be of a certain length.

Browser considerations

Now, as noted, our two heros above are JavaScript 1.5 methods. What this means is that they’ll only work in IE5.5+ and NS6+. The issue of legacy browsers not performing the desired formatting operation not withstanding, how do you ensure that these two methods at least degrade well? Well, by using method detection in your code. For example:

var dummyNumber=2489.8237
if (dummyNumber.toFixed) //if browser supports toFixed() method
dummyNumber.toFixed(2)

Taken From JavaScript Kit