Jump to content

Recommended Posts

Hello Again,

 

I have a data base that is full of averages, for example (.124). They are stored in the database like this (0.124). Of course without the parentheses on each example. I am using an equation to get these numbers and just for the record the equation is not the problem.

 

To get rid of the leading zero in (0.124) I am using ltrim. For example:

 

$num = 0.124;
ltrim($num, 0);

 

Now my equation is giving me negative numbers, which is fine. The problem is that the ltrim command is not working on the negative numbers. The number I am getting when I output is (-0.124). I want the number to be (-.124).

 

Any suggestions??

Link to comment
https://forums.phpfreaks.com/topic/267295-ltrim-negative-numbers/
Share on other sites

Hi,

 

first of all, using ltrim with the value of 0 might lead to problems with significant figures (remember, after the decimal point, every 0 is as important as all the other numbers, because it holds a place. Just as 0.3045 != 0.345

Here's what i would do:

 

    Find the negative numbers (preg_match maybe) and multiply them by -1 and then trim the 0 and multiply by negative one again

I'm afraid both of you are wrong. ltrim () will not alter the 0 in the middle of a number, contrary to what jackr1909 stated. Yours, jazzman1, won't do what the OP wants to do.

 

The correct solution would be to use preg_match (), like jackr1909 was alluding to. Though, not quite in the same way as he thought:

if (preg_match ('/(\\-?)0(\\.\\d+)/', $num, $matches)) {
     $num = $matches[1].$matches[2];
}

 

I want the number to be (-.124).

I'm thinking, that could be easily done, but you need to put a dot between it.

Create a new variable as string and add it a dot symbol. I don't have too much ideas about it  :(

// negative number
$num = 0.124;
print intval('-'.strval($num*1000)); // prints  -124

Jazzman1: I think you should read the OP again, particularly this bit: :P

The problem is that the ltrim command is not working on the negative numbers.

 

As for whether or not my code works:

php > $num = -0.123;
php > if (preg_match ('/(\\-?)0(\\.\\d+)/', $num, $matches)) {
php { var_dump ($matches);
php { $num2 = $matches[1].$matches[2];
php { }
array(3) {
  [0]=>
  string(6) "-0.123"
  [1]=>
  string(1) "-"
  [2]=>
  string(4) ".123"
}
php > var_dump ($num2);
string(5) "-.123"
php > $num = 0.456;
php > if (preg_match ('/(\\-?)0(\\.\\d+)/', $num, $matches)) {
php { var_dump ($matches);
php { $num2 = $matches[1].$matches[2];
php { }
array(3) {
  [0]=>
  string(5) "0.456"
  [1]=>                                                                                           
  string(0) ""                                                                                    
  [2]=>                                                                                           
  string(4) ".456"                                                                                
}                                                                                                 
php > var_dump ($num2);
string(4) ".456"

Yes, I know it's an invalid float (integers are whole numbers), but that's what the OP wants.

I don't know why he'd want that either, but I guess it's purely for output formatting. *Shrugs*

Trick isn't as much as making sense of why the poster would want to do something, at certain times, but what s/he's trying to do in the first place. ;)

I don't know who is wrong and who is not. I just said that this is invalid number - -.124

If the OP wants to return a string from number, why should defined $num = 0.124 as decimal, does not make any sense for me, right?

 

Why is -.124 an invalid number?

 

There's a very simple solution: str_replace()

 

Since the values are always decimals less than 1 and greater than -1, you can simply replace '0.' with '.'

 

echo str_replace('0.', '.', '0.123'); // .123
echo str_replace('0.', '.', '-0.123'); // -.123

 

Of course this would not work if there were numbers such as 10.123

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.