OK lets assume I have
$value = '0.0005304';
I'm struggling to work out how I can remove all instances of 0
until another character is detected.
So in the case above the end result would be 5304
I know I should show code working out however I'm unsure on how to do this so don't know where to start looking.
Any help would be appreciated
** Added **
The 2 numbers are stock market prices , open & close
basically I want to deduct the open from the close
this value will be the difference between the 2
example open = 1.23456 close = 1.23446 is a difference of -0.00010
I would want this represented as -10 points
Hope this explains more
Try the following:
$value = '0.0005304';
$value = (int)str_replace('.','', $value);
var_dump($value); //output 5304
str_replace will remove the . and (int) will cast it to int which will remove all the 0s
Trim off the zeroes, or in your case, the zeroes and the decimal.
$value = ltrim($value, '0.');
I don't know if referring to this as "replace characters" is accurate, though. It seems we're replacing them with nothing.
Related
PHP automatically removes the last zero after the decimal point. Forinstance
$number=29.10;
$echo 'Number='. $number;
This will print
Number = 29.1
instead of
Number = 29.10
I actually need the zero after the decimal place but to no avail.
Why is this so? Any idea or link about this, is very much appreciated.
Thanks
I am trying to make this calculation in php but is giving me wrong result. I think that is right.
And if i do 5000.00 - 100.10 it works, but i want the 5,000.00 to work too.
This is my code:
To create the 5,000.00 i have used number_format(5000, 2).
Aswell to the 100.10
$total = $value1 - $value2;
echo $total;
?>
$total = -95.00
I am trying to make this calculation in php but is giving me wrong result. I think that is right.
And if i do 5000.00 - 100.10 it works, but i want the 5,000.00 to work too.
Please Help...
If you want to do arithmetic on number, you can't have the thousands separator (,). What's happening is 5,000.00 is being read as 5 (it stops interpreting it as a number as soon as it hits the comma) and then you're getting 5 - 100.10 which is -95.10 (I'm thinking you left off the .10 in your example.
You'll need to convert first:
$value1 = floatval(str_replace(',', '', $original_value1))
$value2 = floatval(str_replace(',', '', $original_value2))
I'm assuming here that you have them as strings originally. These remove the comma separator.
It sounds like you're confusing rendering in the UI with calculations.
It's perfectly reasonable for a user to see currencies rendered according to their locale rules (e.g. a String "$1,000.00" in USA), but the calculations in the back need to done on a floating point number (e.g. 1000.0).
So you have to be able to convert back and forth between them. You can't make arithmetic operations work on a String. Better to parse the String to a float, do the operations, then convert that back to String for rendering.
I have a loop that calculates a couple revenue values then adds them together, like this:
$SalesGrowth = $C2012Sales+$C2011Sales;
In some cases, this works, and I get the expected, e.g.: 761.9 + 759.0 = 1520.9
In others, it looks like PHP randomly decides to round incorrectly (??) AND change the units (??) and I get:
8,788.0 + 8,794.3 = 16
What is going on here? I've even tried echoing out the separate sales values separated by a space, and they show up correctly, so the underlying figures aren't wrong.
Interpreted as a number, 8,788.0 is just 8, and parsing stops at the comma.
You'll need some locale-aware number parsing if you want to allow gimmicks like thousands-separators.
Update: If you have the Zend Framework, you can do this:
require_once('Zend/Locale/Format.php');
$locale = new Zend_Locale('en_GB'); // #1
$v = "8,410.5";
$n = Zend_Locale_Format::getNumber($v, array('locale' => $locale,'precision' => 3));
echo 2 * $number; // prints "16821"
Instead of hard-coding the locale, you could try and take it from the environment: new Zend_Locale(setlocale(LC_ALL, ""))
Dude the comma issue....
remove all the commas from the numbers before adding them...
str_replace(",","",$no1);
This is pretty simple... When you ask PHP to use the + operator, it will implicitly convert these strings such as "8,788.0" to an numeric value. Since you have a , character, it terminates the usefulness of the number, and it results in it being interpreted as 8. And so on...
Get rid of the non [0-9.] characters and it will work better.
Notice that 761.9 is a valid number, while 8,788.0 is not (from PHP's point of view).
So 8,788.0 in number context will evaluate as 8, just like 8,794.3. And 8+8 = 16.
To fix this problem, process your data to make numbers formatted properly.
So my problem is that php is not casting these types correctly.. I need to figure out a better way of doing it than number 3, if there is one, and I'd really like to know why PHP treats the comma as a period or something.
$cost = "7,800.00"; // "7,800.00"
$cost = (int) "7,800.00" // int(7)
$cost = (int) preg_replace('/[^-0-9\.]/i', '', "7,800.00"); // int(7800)
Edit, to make things clear to those who don't read: I'm looking for a more elegant way to do this. As far as why this behavior exists, I've already gotten the explanation I was looking for. Thanks.
PHP does not know what to do with the grouping characters (in this case a comma) as it is not a numeric character, period, or e for exponentiation. So the functions stop parsing when the first non-relevant character is read.
Second, have a look at the numeric functions PHP provides: floatval and intval
I have an array for months
$months[01] = 'January';
$months[02] = 'February';
$months[03] = 'March';
$months[04] = 'April';
$months[05] = 'May';
$months[06] = 'June';
$months[07] = 'July';
$months[08] = 'August';
$months[09] = 'September';
$months[10] = 'October';
$months[11] = 'November';
$months[12] = 'December';
Now the array does not output correct value for key 07 & 08.
Try doing print_r($months) you will not get any key value August and zero key index for September.
Though I’m able to solve the problem by removing the leading zero, still I would love to know the reason for same.
Even the PHP editor spots some problem but unable to tell what is the problem.
Thanks
Prepending 0 before a number means PHP parses it as an octal value in the same way that prepending 0x causes it to be parsed as a hexadecimal value. Remove the zero, and it will work fine.
echo 07; // prints 7
echo 010; // prints 8
This is mostly used when specifying unix permissions:
chmod("myfile", 0660);
Except for that it's rarely something that you'd want to do.
This is described in the PHP Manual.
Generally, putting a leading 0 on a number tells the compiler that you've giving an octal number (base 8, not base 10).
In octal, 8 and 9 don't exist (8 is 010, 9 is 011), so you're confusing php.
If you really want a leading zero, you can use strings for your indexes
PHP will treat numbers with a leading 0 as octal numbers, either drop the leading 0 or put the key values in quotes.
The way you form an integer literal is important.
See the structure for decimal literals? Notice how a preceeding zero is not part of that pattern?
Either you have to remove the zeros, or treat your array keys as strings
$months['01'] = 'January';
$months['02'] = 'February';
// etc...
I just realized this after precious minutes of debugging and table head banging.
The problem is that PHP doesn't raise an error or even a warning about having malformed the octal literal integer. It just ignores the part from the error until the end of the literal.
Damn.
PS: Who uses octal numbers? never did, never heard of someone using them for a good reason.
Hexadecimal is great, but octal? damn.
Even for permission I usually do chmod ugo+rwx is more straightforward.