I have a php (v. 7.0.16) script running on the command line with this:
$ct = 1;
foreach($sort_this as $cur_site_id => $dx){
$cur_location = $locations[$cur_site_id]['location'];
$msg = "\033[A\33[2K\r" . 'getting data for %25s (' . $cur_site_id . ') store: %7s %01.2f' . "\n";
echo printf($msg, $cur_location, ($ct . ' / ' . count($sort_this)), number_format((($ct / count($sort_this)) * 100), 2));
$ct++;
}
This loop runs about 40 iterations. The printf statement works with 1 small problem. On the line after the "getting data" line I see a number that increments from 7x-7y as it runs (sometimes it starts at 71, sometimes at 77, etc.). I can't figure out what's causing this number to be printed. So when the loop starts I see something like this on the screen:
getting data for DENVER (531) store: 42 / 42 0.00
77
and when it finishes something like this:
getting data for SEATTLE (784) store: 42 / 42 100.00
79
I found how to print to the same line and clear the data here:
Erase the current printed console line
Is there a way to prevent the 7x codes from showing? Also, what are they?
The problem is on this line:
echo printf(...)
printf() generates a string using its arguments (format and values) and prints it. It returns the number of printed characters (the length of the string it generated).
Your code then passes this value to echo that prints it. This is the source of the extra number in the range of 70-77.
You should either remove the echo or use sprintf() instead of printf().
sprintf() generates the string the same was printf() does but it doesn't print it; it returns it and it is passed as argument to echo that displays it.
printf() returns the length of the outputted string.
See PHP docs:
Return Values: Returns the length of the outputted string.
Just remove echo fom that line.
Related
php function round not working correctly.
I have number 0.9950.
I put code:
$num = round("0.9950", 2);
And I get 1.0? Why?? Why I can't get 0.99?
You can add a third parameter to the function to make it do what you need.
You have to choose from one of the following :
PHP_ROUND_HALF_UP
PHP_ROUND_HALF_DOWN
PHP_ROUND_HALF_EVEN
PHP_ROUND_HALF_ODD
This constants are easy enough to understand, so just use the adapted one :)
In your example, to get 0.99, you'll need to use :
<?php echo round("0.9950", 2, PHP_ROUND_HALF_DOWN); ?>
DEMO
When you round 0.9950 to two decimal places, you get 1.00 because this is how rounding works. If you want an operation which would result in 0.99 then perhaps you are looking for floating point truncation. One option to truncate a floating point number to two decimal places is to multiply by 100, cast to integer, then divide again by 100:
$num = "0.9950";
$output = (int)(100*$num) / 100;
echo $output;
0.99
This trick works because after the first step 0.9950 becomes 99.50, which, when cast to integer becomes just 99, discarding everything after the second decimal place in the original number. Then, we divide again by 100 to restore the original number, minus what we want truncated.
Demo
Just tested in PHP Sandbox... PHP seems funny sometimes.
<?php
$n = 16.90;
echo (100*$n)%100, "\n"; // 89
echo (int)(100*$n)%100, "\n"; // 89
echo 100*($n - (int)($n)), "\n"; // 90
echo (int)(100*($n - (int)($n))), "\n"; // 89
echo round(100*($n - (int)($n))), "\n"; // 90
I don't understand what I am doing wrong. I've got a number, eg 220. Then I need to increase it with for example 11%, so 220 * 11% = 244.2, but my answer is 2420?
I've tried the following:
echo '<br>';
echo $col0 . '<br>'; //outputs 220
settype($col0New, "decimal");
$col0New = ($col0 * '11%') + $col0;
echo $col0New . '<br>'; //outputs 2640 but should be 244.2?
$col0New1 = number_format($col0New,2);
echo $col0New1 . '<br>'; //outputs 2,640.00
Please help.
There's no need for making it any more complex than you need. Basic math allows you to just multiply with a constant, more specific, if you multiply by 1.11, you'll get an increase of 11%.
You can simply do it like this
echo $col0 * 1.11; // Outputs 244.20
You can't use the % as percentage in php. To Accompish what you want, you need to rewrite your percentage to it's decimal form. Ex: 55% will be 0.55
You need to stay with pure numbers: 240 * 1.11. Your '11%' is a string literal, which PHP must fist convert to the number 11, and then multiplies by 240, which explains where 2420 comes from.
.11 is 11%, but you want to add 1 to increase the value to get 111%.
The PHP function number_format produces weird results when using it with math operations.
Run this...
echo number_format(32545000 - 24343400) . '<br>';
echo number_format(32545000) - number_format(24343400);
Why does the second one produce an answer of "8" instead of the correct answer?
number_format(32545000) returns a string: 32,545,000
number_format(24343400) returns a string: 24,343,400
The strings are converted to int and you get:
32 - 24 = 8
I have the following string:
CAE33D8E804334D5B490EA273F36830A9849ACDF|xx|yy|46|13896|9550
which in the code below corresponds to $track_matches[0][0].
The only constant-length field is the first (CAE33D8E804334D5B490EA273F36830A9849ACDF), which is 40 characters long. I am trying to get the values xx and yy which are an unknown length and value along with the rest of the column.
So I am trying something like this:
$seperator= '|';
$end_seed= strpos($track_matches[0][0], $seperator, 41 );
$seeders[$i] = substr($track_matches[0][0], 41, $end_seed - 41);
$end_leech= strpos($track_matches[0][0], $seperator, $end_seed +1 );
echo "end_seed" . $end_seed . " end_leach: " . $end_leech;
$leechers[$i] = substr($track_matches[0][0], $end_seed +1, $end_leech - $end_seed - 1);
The problem I am getting is the line $end_leech= doesn't seem to work properly (and doesn't recognize the $seperator) and retuns the entire line ($track_matches[0][0]) as it's value when echo'd while $end_seed returns the proper value. ... so what's going on why is this happening? howw do i fix it?
try:
$temp = explode("|", $track_matches[0][0]);
That will return an array and you can then reference the vars as $temp[1] (xx) and $temp[2] (yy)
try :
$myString="CAE33D8E804334D5B490EA273F36830A9849ACDF|xx|yy|46|13896|9550";
$splitString=explode('|',$myString);
$xx=$splitString[1];
$yy=$splitString[2];
of course you can replicate manually with strpos, substr etc but will take more effort
I have some PHP code that is dividing two numbers that are pulled from a mySQL database however it is not computing correctly. When I echo $comm and $total_fix individually, the numbers are correct. However, when I echo the division of the two it is not the correct answer. Both numbers are DECIMAL(10,0) data type in the database. Below is the PHP code
$percent_comm = $comm / $total_fix;
$percent_comm = number_format($percent_comm, 2, '.', ',');
echo "<td align=\"center\">".$percent_comm."</td>";
here $comm = 2700, $total_fix = 75 but $percent_comm is computing to be 0.03 when it should be 36
From what I see on your comments, you are getting the $comm variable as a string with a comma, because of the format. I suggest to convert the formatted string into a valid number.
Mean while I'll recomend this:
$comm = '2,700';
$comm = str_replace(',','',$comm);
That remove the comma from your number.
From the variable names, you want to know $comm as a percentage of $total_fix. Your code almost does this: You correctly divide $comm/$total_fix, and it correctly gives you 0.027. But you got it backwards when you checked by hand: 36 is the result of dividing 2700/75 (i.e., $total_fix/$comm)
But to get a percentage, multiply by 100 the result of the division:
(75.0 / 2700) * 100 = 2.7 percent.
That's what your code should be getting.