PHP: get number of decimal digits
$str = "1.23444";
print strlen(substr(strrchr($str, "."), 1));
Show a number to two decimal places
You can use number_format():
return number_format((float)$number, 2, '.', '');
Example:
$foo = "105";
echo number_format((float)$foo, 2, '.', ''); // Outputs -> 105.00
This function returns a string.
How to get whole and decimal part of a number?
$n = 1.25;
$whole = floor($n); // 1
$fraction = $n - $whole; // .25
Then compare against 1/4, 1/2, 3/4, etc.
In cases of negative numbers, use this:
function NumberBreakdown($number, $returnUnsigned = false)
{
$negative = 1;
if ($number < 0)
{
$negative = -1;
$number *= -1;
}
if ($returnUnsigned){
return array(
floor($number),
($number - floor($number))
);
}
return array(
floor($number) * $negative,
($number - floor($number)) * $negative
);
}
The $returnUnsigned
stops it from making -1.25 in to -1 & -0.25
Function to count decimal places with PHP
You'll have to make your own if you really want that function. It shouldn't be all that hard, though - just convert to a string, explode()
on the .
, and then take the length of the second element in the array.
Beware of floating point inaccuracy, though.
Drop decimal place without rounding value
You're trying to round a negative number. Rounding down a negative number with floor()
will increase its absolute value; remember, -23.43(0)
is less than -23.425
!
Instead, you're looking to round up its value with ceil()
:
echo ceil(-23.425 * 100) / 100; // -23.42
This can be seen working here.
Check if number is decimal
You can get most of what you want from is_float, but if you really need to know whether it has a decimal in it, your function above isn't terribly far (albeit the wrong language):
function is_decimal( $val )
{
return is_numeric( $val ) && floor( $val ) != $val;
}
How to cut decimal number using PHP
The expression you're trying to format is this:
($row['OK_05'] / $row['TotalMatch']) * 100
So whichever function you decide to use needs to go around that expression.
As to which function to use, you need to select one that returns a string, not a float.
If you use round
, and your expression returns a float that rounds to a number with two zeros after the decimal point, the trailing zeros will not be displayed in the result. For example, echo round(92.0006829268, 2)
will display 92
, not 92.00
. So don't use round
if you need to be sure that two decimal places are always displayed. round
is a math function, not a formatting function.
floor
is really not useful at all here, as it returns a number with no decimal places.
A simple way is to use sprintf
as shown in some of the other answers.
echo sprintf("%.2f", ($row['OK_05'] / $row['TotalMatch']) * 100);
The first argument to sprintf
is "%.2f"
, which is a format string indicating that the second argument should be displayed as a float with two decimal places. The second argument is your expression.
Using bcdiv
as suggested in the other answer will also work, but it works a little differently that sprintf
and will produce a slightly different result in some cases.
sprintf
will round to the number of decimal places specified, so for example
echo sprintf("%.2f", 926.89 / 10); // outputs 92.69
and bcdiv
will truncate instead, so
echo bcdiv(926.89, 10, 2); // outputs 92.68
Whichever one of those works for you, do that.
How to get number of digits in both right, left sides of a decimal number
I am lost on PHP
semantics big time but I guess the following would serve your purpose without the String usage (that is at least how I would do in Java but hopefully cleaner):
Working code here: http://ideone.com/7BnsR3
Non-string solution (only Math)
Left side is resolved hence taking the cue from your question update:
$value = 12343525.34541;
$left = floor(log10($value))+1;
echo($left);
$num = floatval($value);
$right = 0;
while($num != round($num, $right)) {
$right++;
}
echo($right);
Prints
85
8 for the LHS and 5 for the RHS.
Since I'm taking a floatval
that would make 155.0 as 0
RHS which I think is valid and can be resolved by String functions.
Related Topics
Dynamic Paypal Button Encryption
How to Get PHP to Produce a Backtrace Upon Errors
Php: Possible to Automatically Get All Posted Data
Is There a PHP Function to Convert a Query String to an Array
Disable Laravel's Built-In Error Handling Methods
Scandir() to Sort by Date Modified
How to Check If an Integer Is Within a Range
Laravel 5.2 - Use a String as a Custom Primary Key for Eloquent Table Becomes 0
How to Get Only Images Using Scandir in PHP
PHP Warning: Module Already Loaded in Unknown on Line 0
How to Tell If a Session Is Active
How to Use Regexiterator in PHP
How to Change/Custom Password Field Name for Laravel 4 and Laravel 5 User Authentication
Warning: MySQLi_Error() Expects Exactly 1 Parameter, 0 Given Error