I am using cms - megento. I want to display the price value in following format :
add comma after every 3 digits.
for example :
$price = 987536453 ;
Need to print like 987,536,453.
Try using the number_format function.
By default it prints ',' every 3 digits and cuts decimal:
echo(number_format(1234));
1,234
Edit:
as another answer suggested, there is a simple way to do this using number_format:
echo number_format(1234); // 1,234
Original answer:
try this str_split
$price = 1234;
$price_text = (string)$price; // convert into a string
$price_text = strrev($price_text); // reverse string
$arr = str_split($price_text, "3"); // break string in 3 character sets
$price_new_text = implode(",", $arr); // implode array with comma
$price_new_text = strrev($price_new_text); // reverse string back
echo $price_new_text; // will output 1,234
You can use number_format to group by thousands
You can use either number_format or money_format to do this.
number_format - http://in2.php.net/number_format
money_format - http://www.php.net/manual/en/function.money-format.php
Related
How can I add string to another string after a specific character in PHP? Strings are coming from Database.
$stringDB= "FZE-17-01";
$string_add="RTL";
Final output= FZE-RTL-17-01
I tried functions but I don't want to use a position based function like substr_replace after 4 characters, etc. Any good alternative. $string_add after first -
One of many variants is to use array_splice
$arr = explode('-', $stringDB);
array_splice($arr, 1,0, $string_add);
echo implode('-', $arr);
Hope this could help you.
$stringDB= "FZE-17-01";
$string_add="RTL";
echo $newstr = substr_replace($stringDB, $string_add, 4, 0);
PHP substr_replace
I have a STRING $special which is formatted like £130.00 and is also an ex TAX(VAT) price.
I need to strip the first char so i can run some simple addition.
$str= substr($special, 1, 0); // Strip first char '£'
echo $str ; // Echo Value to check its worked
$endPrice = (0.20*$str)+$str ; // Work out VAT
I don't receive any value when i echo on the second line ? Also would i then need to convert the string to an integer in order to run the addition ?
Thanks
Matt
+++ UPDATE
Thanks for your help with this, I took your code and added some of my own, There are more than likely nicer ways to do this but it works :) I found out that if the price was below 1000 would look like £130.00 if the price was a larger value it would include a break. ie £1,400.22.
$str = str_replace('£', '', $price);
$str2 = str_replace(',', '', $str);
$vatprice = (0.2 * $str2) + $str2;
$display_vat_price = sprintf('%0.2f', $vatprice);
echo "£";
echo $display_vat_price ;
echo " (Inc VAT)";
Thanks again, Matt
You cannot use substr the way you are using it currently. This is because you are trying to remove the £ char, which is a two-byte unicode character, but substr() isn't unicode safe. You can either use $str = substr($string, 2), or, better, str_replace() like this:
$string = '£130.00';
$str = str_replace('£', '', $string);
echo (0.2 * $str) + $str; // 156
Original answer
I'll keep this version as it still can give some insight. The answer would be OK if £ wouldn't be a 2byte unicode character. Knowing this, you can still use it but you need to start the sub-string at offset 2 instead of 1.
Your usage of substr is wrong. It should be:
$str = substr($special, 1);
Check the documentation the third param would be the length of the sub-string. You passed 0, therefore you got an empty string. If you omit the third param it will return the sub-string starting from the index given in the first param until the end of the original string.
I have a get variable in this format : 0-1499. Now I need to convert it to a string so that I can explode the variable. For this I tried to convert it to string , but I am not getting any output. Here is the sample code :
$mystring = $_GET['myvars']; //equals to 0-1499;
//$mystring = (string)$mystring;
$mystring = strval($mystring);
$mystring = explode("-",$mystring);
print_r($mystring);
The above print_r() shows an array Array ( [0] => [1] => 1499 ). That means it calculates the $mystring before converted into string. How can I send 0-1499 as whole string to explode ?
I have a get variable in this format : 0-1499
When you grab this variable from the URL say.. http://someurl.com/id=0-1499
$var = $_GET['id'];
This will be eventually converted to a string and you don't need to worry about it.
Illustration
FYI : The above illustration used the code which you provided in the question. I didn't code anything extra.
You need quotes, sir.
Should work fine like this.
$mystring = "0-1499";
$mystring = explode("-",$mystring);
print_r($mystring);
Without the quotes it was numbers / math.
0 minus 1499 = negative 1499
As you correctly note it treats the value as arithmetic and ignores the 0- part. If you know that the value you'll get is 0-n for some n, all you need to do is this:
$mystring="0-".$n;
$mystring=explode("0-", $mystring);
but explode here is a bit redundant. So,
$myarr=array();
$myarr[1]=strval($mystring);
$myarr[0]="0";
There you go.
Explode is used for strings.http://php.net/explode
<?php
$mystring = "0-1499";
$a=explode("-",$mystring);
echo $a[0];
echo "<br>";
echo $a[1];
?>
see it working here http://3v4l.org/DEstD
I have script that identifies with preg_match_all some numbers from a given file and in a given format '#(\d\,\d\d\d\d)#' (decimal, with 4 decimals). With them, later, I need to do some math operations to find out the sum, average etc.
With print_r I can see all matches from the array and it is ok (4,3456, 4,9098, etc.). I verify the type of variables and gettype() returned string
Unfortunately I cannot do math operations with them because when I use the variables in a math expression the result is always rounded regardless of what came afer the comma.
For example:
4,3456 + 4,9098 + 4,3456 = 12, or 12,0000 -- if I use number_format.
I used . instead of , in the numbers, I formatted the results with number_format, but have had no success. It seems I am missing something.
Thanks for help!
The error happens even before the number_format call -- PHP considers . as the decimal separator, not ,. you need to str_replace all your array elements:
$values_array = str_replace(",", ".", $values_array)
PHP uses the . character as decimal separator, so you have to replace the , by a . in your matched numbers before converting them to numbers:
$number = floatval(strtr("1,234", ",", "."));
// 1.234
Example:
<?php
$numbers = array("1,234", "5,67");
$numbers = str_replace(",", ".", $numbers);
echo number_format($numbers[0] + $numbers[1], 4, ',', ' ');
Try it here: http://codepad.org/LeeTiKPF
I'm doing a little application of adding prices and decimals. Points are normal to use with decimals, but how can I write decimal number with comma as input (543,35 instead of 543.35) and then maybe change it with point to the database (mysql)? Then print it back form the database with the comma. Reason is that comma (,) is more used in Finland than point (.) when write decimal numbers.
Thank you very much!
Samuel
$input = '5,50';
$output = str_replace(',', '.', $input);
var_dump($output); // string(4) "5.50"
$number = (float)$output;
var_dump($number); // float(5.5)
you need not do anything in the sql end. you want to format the decimal value in php (this assumes php4/php5): Set the third parameter $dec_point to ','
// string number_format ( float $number , int $decimals , string $dec_point , string $thousands_sep )
<?php
$number = 1234.56;
// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56
$number = 1234.5678;
// english notation without thousands seperator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57
?>
source:
http://php.net/manual/en/function.number-format.php
Cheers!
the PHP number_format function is what you need :
number_format(5.50, 2, ',');
...should do the trick.
As it's been said, it's best to save the value as a float and then format for display. As an alternative to the previously suggested number_format(), you could try money_format() http://us.php.net/manual/en/function.money-format.php It doesn't work in a windows environment though if that is important to you.
No, using comma as a decimal separator for arithmetic operations is not supported. Filter your input and replace the comma with a period, then format your output as you wish.