I have problems to get my percent counter function to get it work with English money format
function calc_proc($price, $savings) {
$old_price = $price + $savings;
return (number_format($savings / ($old_price/100)));
}
because of the commas I'm getting bad values
Is english money format something like this: 253,17?
If yes, then simply do:
str_replace(',', '.', $value);
Then you're safe.
First of all you don't have to use strings in financial calculations but if you still have to use, you should replace commas with dots. For example,
$a = '1,5';
$b = '2,1';
echo $a + $b; //result 3
// you can avoid this, by replacing comma:
$a = str_replace(',', '.', $a);
//same for $b
Another solution could be setting locale but you'd have issues with dots after that.
In your function it'll look like this:
function calc_proc($price, $savings) {
$price = str_replace(',', '.', $price);
$savings = str_replace(',', '.', $savings);
$old_price = (float)$price + (float)$savings;
return (number_format($savings / ($old_price/100)));
}
The PHP number_format() function without parameters return english format with comma. You should instead use:
return round($savings / ($old_price/100), 0);
and make sure $old_price cant be 0
function percent($price, $savings) {
$count1 = str_replace(',', '.', $price)/ $savings;
$count2 = $count1 * 100;
$count = number_format($count2, 0);
return $count;
}
Try setlocale(constant, location) at the beginning of your script. In your case:
setlocale(LC_ALL,"En-Us");
Related
In php how to replace a '/' and find in between numbers
Example:
$a = 30/36;
// i need to get $a value is 30,31,32,33,34,35,36 using php
print_r(range(...explode('/', '30/36')));
Try This
$a = '30/36';
$explode=(explode("/",$a));
print_r($explode);
$range=(range($explode[0],$explode[1])) ;
echo implode(",",$range);
Simple google search would give you the answer. ..
$a = '1/2/3/4/5';
$res = str_replace('/', ',', $a);
echo $res;
EDIT: As per #NigelRen request! Took part from #u_mulder (i think acceptable answer) to complete my answer!
$a = '30/36';
$res = str_replace('/', ',', $a);
$range = range(...explode(',', '30,36'));
$range = implode(',', $range);
echo $range;
using php. I have the following number
4,564,454
454,454,454
54.54
65.43
I want to convert these into number for calculating. How can I do it? Right now, the type of these number is string.
Note: the comma is not a separate of a number, it is a notion to make a number nicer. I got this format from the ajax request, I cant change the format though. So, I have to use it.
Thanks
$var = floatval(str_replace(",", "", "454,454,454"));
$a='4,5,4';
$ab= explode(',', $a);
foreach ($ab as $b)
{
$sum+=$b; //perform your calculation
}
echo $sum;
First you need to remove ,(comma) from your string as below :
$str=str_replace(",", "", "454,454,454");
Then converting in numeric:
$int = (int)$str;
or
$int=intval($str);
now do your calculation using $int variable.
try this code
$str = '4,564,454';
$str2 = '454,454,454';
$str3= '54.54';
$str4= '65.43';
$sum=0;
$sum += array_sum(explode(',',$str));
$sum += array_sum(explode(',',$str2));
$sum += $str3;
$sum += $str4;
echo $sum;
I have a string I get from a website.
A portion of the string is "X2" I want to add +1 to 2.
The entire string I get is:
20120815_00_X2
What I want is to add the "X2" +1 until "20120815_00_X13"
You can do :
$string = '20120815_00_X2';
$concat = substr($string, 0, -1);
$num = (integer) substr($string, -1);
$incremented = $concat . ($num + 1);
echo $incremented;
For more informations about substr() see => documentation
You want to find the number at the end of your string and capture it, test for a maximum value of 12 and add one if that's the case, so your pattern would look something like:
/(\d+)$/ // get all digits at the end
and the whole expression:
$new = preg_replace('/(\d+)$/e', "($1 < 13) ? ($1 + 1) : $1", $original);
I have used the e modifier so that the replacement expression will be evaluated as php code.
See the working example at CodePad.
This solution works (no matter what the number after X is):
function myCustomAdd($string)
{
$original = $string;
$new = explode('_',$original);
$a = end($new);
$b = preg_replace("/[^0-9,.]/", "", $a);
$c = $b + 1;
$letters = preg_replace("/[^a-zA-Z,.]/", '', $a);
$d = $new[0].'_'.$new[1].'_'.$letters.$c;
return $d;
}
var_dump(myCustomAdd("20120815_00_X13"));
Output:
string(15) "20120815_00_X14"
I have this string:
467:some-text-here-1786
How can I select only the first numerical value before the ":" ?
Thank you
Very simple:
list($var) = explode(":",$input);
or
$tmp = explode(":",$input);
$var = array_shift($tmp);
or (as pointed out by PhpMyCoder)
$tmp = current(explode(":",$input));
$string = '467:some-text-here-1786';
$var = (int)$string;
Since you're extracting a number, this is enough :)
For an explanation of why this works, check the official PHP manual: http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion
It's also really fast and really safe: you are sure you get a number.
$a = "467:some-text-here-1786";
$a = explode(":", $a);
$a = $a[0];
another way to do this:
$length = strpos($string, ':') + 1;
$number = substr($string, 0, $length);
I have a whole bunch of percentages stored as XX% (e.g. 12%, 50%, etc..) I need to remove the percentage sign and then multiply the percent against another variable thats just a number (e.g. 1000, 12000) and then output the result. Is there a simple way to strip the percentage sign and then calculate the output with PHP? Or should I consider some sort of JS solution?
You could use rtrim():
$value = ((int) rtrim('12%', '%')) * 1000';
Edit
You don't strictly need to call rtrim() , as it casts to an int ok with the percentage sign. It is probably cleaner to strip it though.
var_dump (12 === (int) '12%');
//output: bool(true)
You can make use of preg_replace_callback as:
$input = '12%, 50%';
$input = preg_replace_callback("|(\d+)%|","replace_precent",$input);
echo $input; // 12000, 50000
function replace_precent($matches) {
return $matches[1] * 1000;
}
Try this:
$number = str_replace('%', '', '100%');
$result = intval($number) * 5000; // or whatever number
echo $result;
If you use trim() or str_replace() in PHP you can remove the percent sign. Then, you should be able to multiply the resulting number (php is weakly typed after all).
<?php
$number = str_replace("%", "", $percentString);
$newNumber = ((int) $number) * 1000;
echo $newNumber;
?>
You can use str_replace. You can also pass an array of subjects into str_replace to have them all replaced.
<?php
$number = str_replace("%", "", $percentage);
$result = $number * $other_var;
print $result;
?>
<?php
$input=array('15%','50%','10.99%','21.5%');
$multiplier=1000;
foreach($input as $n){
$z=floatval($n)*$multiplier;
print("$z<br>");
}
?>