How to get rupees and paise in separate columns from the amount - php

The database value for my project will be TOTAL AMOUNT like "1500.45". What I want is to separate the column for Rupees & Paise. 1500 should be under Rupees column, and paise 45 should be under paise column. How to do that?
Output should be like this
|amount | | rupees | paise |
|1500.45| ==> | 1500 | 45 |

simply using split comment to separate rupees and paise..
<?
$RS=2300.75;
$df=split("[.]",$RS);
echo "rupees ".$df[0];
echo "<br>";
echo "paise ".$df[1];
?>
for more details see the link php code for rupees and paise

If you need two need for two decimal points then use the following code :
$amount = 1500.45; // if it is a string then convert it into decimal
$rupee = floor($amount);
$paise = (int)($amount - $rupee)*100; // and set pricision two
echo $rupee.'<br/>';
echo $paise;
and if it string or decimal any type then you can use the code as follows :
$amount = 1500.45;
$rupee = explode('.',$amount)[0];
echo $rupee.'<br/>';
echo explode('.',$amount)[1];

Use explode() Function .
explode(".",your amount)
explode will separate your amount to get Rupees & Paise .
You will get separated value in array using that array you can store bot values in separate columns .

use SUBSTRING_INDEX() to split values in mysql then update
UPDATE `table` SET `rupees`=SUBSTRING_INDEX(`amount`,'.',1),`paise`=SUBSTRING_INDEX(`amount`,'.',2)

Select substring_index('amount', '.', 1) as rupees, substring_index('amount', '.', -1) as paise;

Related

How to show decimal value after multiplication

I have value which stored in database like price. 123.00
When i tried to see the subtotal which is quantity * price.
So 123.00*1 = 123.00
But in view it shows 123 instead of 123.00 where price showing like 123.00 in same table.
How can i show decimal value after multiplication?
You need to format the number using number format PHP function. Consider
$price = 125;
$quantity = 1;
echo number_format($quantity*$price,2);
You can also have extra comma separated values that set the decimal separator, i.e. "." and the thousands separator also i.e. ",". See http://php.net/manual/en/function.number-format.php

PHP & MySQL - Generate invoice number from an integer from the database

I need to generate an invoice number from an integer of a table with an auto incrementing ID of the database where the user purchases saved.
Example of the table invoice database:
The invoice number format floor do one of two ways.
Example 1: of the number of invoices without prefix:
0000001 |
0000002 |
0000003 |
0000004 |
0000005
Example 2: the number of invoices with prefixes:
F-0000001 |
F-0000002 |
F-0000003 |
F-0000004 |
F-0000005
Question:
1) ¿What is the best way to do this, you can do directly from MySQL or PHP?
2) ¿What is the most appropriate format Example 1 or Example 2?
I appreciate your support as always!
Thanks to Gordon Linoff, I could get a way to solve this.
I will share an example, perhaps someone may be interested.
SQL - Invoice without prefix: SELECT id, LPAD(id,7,'0') FROM invoice WHERE id = 1;
Result: 0000001
SQL - Invoice with prefix: SELECT id, CONCAT( 'F-', LPAD(id,7,'0') ) FROM invoice;
Result: F-0000001
You can write a good helper function in PHP to use it wherever you want in your application to return an invoice number. The following helper function can simplify your process.
function invoice_num ($input, $pad_len = 7, $prefix = null) {
if ($pad_len <= strlen($input))
trigger_error('<strong>$pad_len</strong> cannot be less than or equal to the length of <strong>$input</strong> to generate invoice number', E_USER_ERROR);
if (is_string($prefix))
return sprintf("%s%s", $prefix, str_pad($input, $pad_len, "0", STR_PAD_LEFT));
return str_pad($input, $pad_len, "0", STR_PAD_LEFT);
}
// Returns input with 7 zeros padded on the left
echo invoice_num(1); // Output: 0000001
// Returns input with 10 zeros padded
echo invoice_num(1, 10); // Output: 0000000001
// Returns input with prefixed F- along with 7 zeros padded
echo invoice_num(1, 7, "F-"); // Output: F-0000001
// Returns input with prefixed F- along with 10 zeros padded
echo invoice_num(1, 10, "F-"); // Output: F-0000000001
Once you are done writing the helper function, you don't need to use LPAD or CONCAT MySQL functions every time in your query to return ID with padding zeros or zeros with prefix. If you have global access to the helper function in the entire application, you only need to invoke it wherever you want to generate an invoice number.
1 - 0000001 | 0000002 | 0000003 | 0000004 | 0000005
$dbValue = 1;
echo $dbValue = str_pad($dbValue, 7, "0", STR_PAD_LEFT); // it will give 0000001;
2 - F-0000001 | F-0000002 | F-0000003 | F-0000004 | F-0000005
$dbValue = 1;
echo $dbValue = "F-".str_pad($dbValue, 7, "0", STR_PAD_LEFT); // it will produce F-0000001;
Fetch last ID from database and store it in a PHP variable.
For example, if last record is 100, then increment it by 1.
$last = 100; // This is fetched from database
$last++;
$invoice_number = sprintf('%07d', $last);
Finally, the answer for second question is,
$number = "F-". $number;
Ans 1):
You can do this with PHP(directly by concat or use str-pad ) as well as with MySQL( LPAD ) also
But as per my view you should do this by PHP, so that you can change it according to your requirements e.g. extend zeroes as per number of id's in DB.So that not to change SQL queries and make it heavy.
Ans 2):
You can use both formats but if you want to be more specific about particular user or any thing else, then use second format.
I think second format can give you more information about data

How do I calculate percentage in php mysql table?

I have a question related to a calculation of the percentage with php using mysql database.
I have three table month, revenue, overhead.
EX:
Month | Revenue | Overhead
Jan | 2500 | 1000
Feb | 1000 | 500
I would like to demonstrate the difference from one value to another using a percentage with php mysql. Now I need to show the values ​​already calculated, do that to more like php?
Sorry for my english.
$revenue = $row['revenue'];
$overhead = $row['overhead'];
$_div100 = $revenue / 100;
$percentage = $overhead / $_div100;
echo number_format($percentage, 2);
Once you receive the data back from the database in an array.. lets say in a variable called $data
you could do
<?php
foreach($data as $value){
$percentage = $value['Overhead'] / $value['Revenue'] * 100;
echo $value['month'] . " - " . $percentage . "%<br>";
}
?>

Comma separated numeric values - PHP CodeIgniter

How would I echo the numeric values as comma separated
foreach($report_value as $value):
echo round($value['report_total_cost_usd'],2); // ex: 1,234,456.00
endforeach;
This is what you are looking for number_format
echo number_format($value['report_total_cost_usd'], 2, '.', ','); // ex: 1,234,456.00
Output
1,234,456.00
need some dedication towards the work, and some knowledge how to read manuals..
MySQL Solution:
Use format function on the number with an input for decimal places.
Example:
mysql> select format( 1234456, 2 );
+----------------------+
| format( 1234456, 2 ) |
+----------------------+
| 1,234,456.00 |
+----------------------+
You can read the same as string from your scripting language and use.
Refer to:
MySQL: FORMAT(X,D[,locale])
Formats the number X to a format like '#,###,###.##', rounded to D
decimal places, and returns the result as a string. If D is 0, the
result has no decimal point or fractional part.
Try this one
foreach($report_value as $value)
{
echo number_format($value['report_total_cost_usd'],2,'.', ','); // ex: 1,234,456.00
}

Formatting numbers from 1000 to 10.00

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().

Categories