1

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

4

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

2

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.

4 Comments

I hate that this works lol. 3v4l.org/11V8A Can you explain why the other 0's get trimmed even tho you're only telling it to trim 0.?
This is a much better solution. Even though OP did not specify, this will work for 3.00052 or the like, whereas the other answer won't.
@WillParky93 The second argument is a character mask. It will trim any included characters. For '0.' It trims 0 and ..
I'll elaborate a bit more in what this is intended for in the origianl post, give me a minute :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.