I am new to php. Can anyone tell how to change the query such that it will print 16.66. I need 16.66667 to be only 16.66.
<?php
$number = 16.6666667;
$n = number_format($number, 2);
echo $n; // 16.67
?>
I am getting output as 16.67. Thanks for any help.
Use the following code.
$number=floor(16.6666667 * 100) / 100;
This will return value without rounding the number.
$n = substr($number, 0, strrpos($number, '.') + 3);
EDIT
For any number with or without decimals, try this:
$n = number_format($number, 3);
$n = substr($n, 0, strrpos($n, '.') + 3);
Related
For an exercise I wrote this little loop to convert decimal number into a binary one. It returns the right value, I tested it, but it appends a hell lot of zeros to the output.
$dec = 17;
$bin = null;
while($dec != 0)
{
$bin .= $dec % 2;
$dec /= 2;
round($dec, 0, PHP_ROUND_HALF_DOWN);
}
echo $bin;
I really can't tell why this is, am I blind? Can you give me an explanation? I would appreciate it.
I tried to test for $dec == 0 and if that succeeds break; like so:
while($dec != 0)
{
$bin .= $dec % 2;
$dec /= 2;
round($dec, 0, PHP_ROUND_HALF_DOWN);
if($dec == 0)
{
break;
}
}
That didn't work. So I'm searching for a solution.
Thanks for the help in advance!
Jona
The problem with your loop is that $dec /= 2 is not an integer division. If you print out $dec in your loop you will see the values are 8.5, 4.25, 2.125, ... and so on until eventually $dec becomes the smallest possible double precision value, at which point in the next loop it finally becomes 0. Although you are calling round, which should fix that, you are not assigning the output of round to $dec so it has no effect. It's probably preferable to do an integer division to begin with. In PHP7 and up you can use intdiv:
$dec = intdiv($dec, 2);
Or in any PHP version a simple right shift works:
$dec = $dec >> 1;
Otherwise you can use intval, floor or round or an (int) cast to convert the floating point result to an integer:
$dec = intval($dec / 2);
$dec = floor($dec / 2);
$dec = round($dec / 2, 0, PHP_ROUND_HALF_DOWN);
$dec = (int)($dec / 2);
Demo on 3v4l.org
The other problem is that you need to push the values into the $bin string in reverse order, so you need to replace
$bin .= $dec % 2;
with
$bin = ($dec % 2) . $bin;
So an example loop would be:
$dec = 24;
$bin = null;
while($dec != 0)
{
$bin = ($dec % 2) . $bin;
$dec = $dec >> 1;
}
echo $bin . PHP_EOL;
Output:
11000
Okay, I've found your problem: if you had debugged your loop, you would have seen that $dec is just halfed in each step. The call to round does not do anything, as you don't use the returned value. Just assign that to $dec
I have:
$int = 11487171;
I want to round it to the first 3 digits, making it 115. What's the PHP function to do this? Apologies for entry level question.
If the number has 3 or less digits, just output it as, else divide it by 10^(digits-3) and round the result.
Could be easily done with strlen, and round.
$len = strlen($int);
if ($len <= 3) {
echo $int;
} else {
echo round($int / pow(10, $len - 3), 0);
}
Try it using substr:
$digit = 11487171;
function makeit($digit){
return $digit? substr($digit, 3, 1)>=5?
substr($digit, 0, 3)+1:substr($digit, 0, 3):
substr($digit, 0, 3);
}
echo makeit($digit);//print 115
What's the simple way to remove more than 1 decimal number from source number .
for example source numbers are :
1st source number is : 56.48216585224
2nd source number is: 93
Output must be :
1st output : 56.4
2nd output: 93
numbers are not static
what's the simple way ?
If you don't want rounding, then:
$number = 56.48216585224;
echo substr($number, 0, strpos($number, '.')+2); // output: 56.4
Otherwise:
Use php round() or number_format()
http://php.net/manual/en/function.round.php
http://php.net/manual/en/function.number-format.php
Examples:
$number = 56.48216585224;
echo number_format($number, 1, '.', ''); // Output: 56.5
echo round($number, 1); // Output: 56.5
I will suggest this PHP code for your requirement:
$n = 56.48216585224;
$m = floor($n * 10) / 10; // $m = 56.4 now
You can try the following
print(quickFormat("56.48216585224"));
print(quickFormat("93"));
function quickFormat($number) {
$number = explode(".", $number);
if (isset($number[1])) {
return $number[0] . "." . $number[1]{0};
} else {
return $number[0] ;
}
}
Output
56.4
93
$temp is currently 6. But the variable result can be changing every time to a different number so it is not a fixed value.
Anyway, for this $temp * 1.1666666, the result will be 6.99999996. Since I used the floor function, it will be rounded down to 6.
Is there any way when the value is more then>*.49999 it will stay at *.5 instead of *?
Example: 6.51111111, 6.78948123, 6.9747124
Expected Output: 6.5
Example: 6.49999999, 6.12412431, 6.33452361
Expected Output: 6
Do note that, $temp value will be ever changing..thank you!
Use round($number, 1). That will round to the nearest decimal point.
$number = round(.1666666 * $temp, 1);
If you want to round to the nearest half you can do this:
function round_to_half($num)
{
if($num >= ($half = ($ceil = ceil($num))- 0.5) + 0.25) return $ceil;
else if($num < $half - 0.25) return floor($num);
else return $half;
}
$number = round_to_half(.1666666 * $temp);
Try this code...
<?php
$temp = 6.94444;
echo myRound($temp);
function myRound($temp)
{
$frac = $temp - floor($temp);
$frac = ($frac >= .5) ? .5 : 0;
return ( floor($temp) + $frac );
}
?>
Hope this is what you want.
I hold decimals in a database using DECIMAL(10,5)
I would like to format these numbers according to a few rules:
A zero decimal should display as 0
Show a long decimal (no trailing zero's) with all of it's numbers
When possible, I would like to only show up to 2 decimal places (when there are trailing zeros)
Here are some examples:
The left side corresponds to how the number is stored in database.
The right number is how I would like to display the number in my application.
0.00000 => 0
0.51231 => 0.51231
0.12000 => 0.12
0.40000 => 0.40
0.67800 => 0.678
12.10000 => 12.10
This will work for you:
function format($x){
if(!(int)substr_replace($x, '', $dpos = strpos($x, '.'), 1))
return 0;
else
return str_pad((rtrim($x, '0')), $dpos + 3, '0');
}
Example
I would utilize the number_format function in php to actually do the formatting after you determine the amount of decimal places to the number has.
Source:
http://php.net/manual/en/function.number-format.php
Example Usage:
$number = 1234.56;
// english notation (default)
$english_format_number = number_format($number);
// 1,235
// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56
$number = 1234.5678;
// english notation without thousands separator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57
Well here's one way (I haven't tested it yet so there may be minor errors):
$pattern = '/([0-9]+)\\.{0,1}([0-9]*?)0*$/';
$subject = 12.10000;
$matches = array();
$result = preg_match ($pattern, $subject, $matches);
$number = $matches[1];
if ($matches[2] != 0) {
$number .= '.'.$matches[2];
if ($matches[2] < 10) {
$number .= '0';
}
}
echo $number;
And here's another way (probably a little faster):
$x = 1.000;
$result = (int)$x;
$trimmed = rtrim($x, 0);
if ($trimmed[strlen($trimmed) - 1] != '.') {
if ($trimmed[strlen($trimmed) - 2] == '.') {
$result = $trimmed.'0';
} else {
$result = $trimmed;
}
}
echo $result;
I haven't used it myself, but theres the NumberFormatter class: http://php.net/manual/class.numberformatter.php as part of the Internationalization Functions for this stuff. Using that is a little more involved i think though.
I know this is an old question, but the following quick function I wrote for my own project might help someone looking for this.
function number_format_least_dp($number, $decimal_point = '.', $thousand_seperator = ','){
if (floatval($number) == (int)$number){
$number = number_format($number, 0, $decimal_point, $thousand_seperator);
} else {
$number = rtrim($number, '.0');
$number = number_format($number, strlen(substr(strrchr($number, '.'), 1)), $decimal_point, $thousand_seperator);
}
return $number;
}