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%.
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
$quantity = 20;
$product_rate = 66.79;
$total = $quantity * $product_rate;
echo $total;
Output is showing 1335.8000000000002
is there possible to show 1335.8 using php..?
You can use the number_format() function like this:
$firstNum = 1335.8000000000002;
$number = number_format($firstNum, 1, '.', '');
echo $number;
outputs:
1335.8
more on number_format() here: http://php.net/number-format.
You can also multiply the number by 10, then use intval() to convert it to an integer (that way stripping out the decimals) and then divide by 10 like this:
$firstNum = 1335.8000000000002;
$number = 10 * intval($firstNum)/10;
echo $number;
outputs:
1335.8
Note: when using the methods above there will be no rounding, for rounding you would use something like this:
$number = round($firstNum, 1);
echo $number;
which in this case also outputs:
1335.8
Do you really use these variable values? I'm using PHP7 and the output for your given values is 1335.8. If you do a manual calculation it is the same result. It should be 1335.8. Anyway if you need to roundup the value you can use below.
round($total,1);
Please refer the below link and you will be able to grab more details.
http://php.net/manual/en/function.round.php
Because how floating point numbers work, they cannot represent every numbers exactly, so approximations are made.
The closest representation of 20 is 20, it can represent 20 exactly, but 66.79 for instance is approximated to 66.7900000000000062527760746889, that times 20 is 1335.800000000000125055521493778 that again cannot be represented and is approximated to 1335.80000000000018189894035459.
Depending on how you choose to print this number, it may round different ways, in your case for some reason you decided to print 13 decimal places so it rounded to 1335.8000000000002, but if you print only 1 or 2 decimal places it will print as 1335.8 or 1335.80. Just be mindful about that when printing floating point numbers, you may want to specify how many decimal places are relevant to you. For that, use number_format().
Example:
echo number_format($number, 2); // prints 2 decimal places
You can do this simply using echo echo round($total, 1) instead of doing round($total)
I'll like to format 1000 to 10.00
The PHP number_format function does not seem to be working for this.
I have tried:
$amount2 = number_format("$cost",2,"",",");
echo "$cost";
Any ideas? Is there a way I can manupulate number_format to display the results (i.e just inserting a decimal before the last two digits?
Number format will change the "." to a "," but you telling it to format ONE THOUSAND.
$cost=1000;
echo number_format($cost,2,'.',',');
//1,000.00
What you want is simply:
$cost=1000;
echo number_format($cost/100,2,'.',',');
//10.00
Is this legit for you ?
<?php
$cost=1000;
echo substr($cost, 0, 2) . "." . substr($cost, 2);//10.00
1000 and 10.00 are totally different numbers (in values). Divide by 100, then format it properly:
$cost = 1000 ;
$cost /= 100 ;
$amount2 = number_format($cost,2,".","");
echo $amount2 ;
Try this code:
$stringA= 1000;
$length=strlen($stringA);
$temp1=substr($stringA,0,$length-2);
$temp2=substr($stringA,$length-2,$length);
echo $temp1.".".$temp2; // Displays 10.00
The third parameter to number_format should be the character you want to use as a decimal point. Why are you passing an empty string? And why are you placing your number ($cost) inside a string?
Try this: echo number_format($cost,2,'.',',');
EDIT: Perhaps I misunderstood your question — if you want the number 1000 to be displayed as 10.00, just divide $cost by 100 before calling number_format().
Have such mysql query SELECT CurrencyRate/Units AS FinalCurrencyRate
Value for CurrencyRate is 0.06200000 and value for Units is 1000.
So 0.06200000 / 1000 and get 6.2E-5
The same result if echo $result = 0.06200000 / 1000 . '<br>';
If echo $result = number_format( (0.06200000 / 1000), 10, '.', '' ) . '<br>'; then can get 0.0000620000
What is solution for mysql query to get normal number instead of 6.2E-5?
Found round(CurrencyRate/Units,10)
But what if do not know number of 00000 after decimal? For example 0.06200000 / 1000000000000000000
use type DECIMAL for such values. This would make your values display the way you mentioned.
Because you are working with currencies, use BC Math Functions for better percision. You can set the number of digits after the decimal place in the result when you use them. Example:
//bcdiv - it divides to numbers
echo bcdiv('105', '6.55957', 3); // 16.007
May I know what is the function to be use in order to round up the column value into 2 decimals point with percentage symbol? E.g: 1.88% instead of 1.88230293
$worksheet_details->setCellValue("D14", "=SUM((F33 / F34))");
How do I round up the value in cell D14?
SOLUTION:
By the way, after I keep continue look for the solution from the Internet and I got this...
$percentageFormat = '#.## \%;[Red]-#.## \%';
$worksheet_details->setCellValue("C14", "=SUM((C33 / C34) * 100)");
$worksheet_details->getStyle('C14')->getNumberFormat()->setFormatCode($percentageFormat);
just change the first # to 0 if you want it display in 0.xx format... or else it will display .xx only
If you just want it to be outputted as a string:
echo '%'.number_format($your_number,2);
If you want to retain it as a numerical float value (but compromise on the percentage):
echo round($your_number,2);
If you're looking for an Excel function, use:
$worksheet_details->setCellValue('D14', '=TEXT(F33/F34,"0.00%")');
I guess you are looking for ROUND() function:
=ROUND(10/3; 2)
or, in your case:
$worksheet_details->setCellValue("D14", "=ROUND(SUM((F33 / F34)); 2)");
If you mean to round in PHP, with round() you can specify the precision you want a float number:
echo round(1.95583, 2) . "%"; // 1.96%
If you mean to round in Excel, you can use the ROUND function:
$worksheet_details->setCellValue("D14", "=TEXT(SUM((F33 / F34)), '###.##%')");
or
$worksheet_details->setCellValue("D14", "=TEXT(SUM((F33 / F34)/100), '###.##%')");
if the number is a percentage already.
Hope it helps.
If you want to display in percent number format you can try this.
$percentVal = '95.6';
PHPExcel_Cell::setValueBinder( new PHPExcel_Cell_AdvancedValueBinder() );
$percentCell->setValue($percentVal . '%');
$sheet->getStyleByColumnAndRow($percentCellIndex, $dataRow)->getNumberFormat()->setFormatCode('0.0%');
// Output 95.6% and formatted as number in Excel
As #ariefbayu said,
$worksheet_details->setCellValue("D14", "=ROUND(SUM((F33 / F34)); 2)");
should work, but to me it threw the error:
PHP Fatal error: Uncaught PHPExcel_Calculation_Exception
so i changed the ; for a ,.
result:
$worksheet_details->setCellValue("D14", "=ROUND(SUM((F33 / F34)), 2)");
You can set the format of your cell to automatically round value to whole number.
$activeSheet->getStyle("A1")->getNumberFormat()->setFormatCode('##');