Tripp
Tripp

Reputation: 67

Support negative values

I'm in a situation where the CSV file is getting rid of the leading zero before my import and I need to be able to account for that. Let's say that I have my value as the following:

-.0982739 -> I would want all case scenarios where it's -. to turn into -0. - Here are my attempts:

if (str_contains($this->longitude, '-.')) {
    $this->longitude = '0' . $this->longitude;
};
Outputs: 00-.0989070

if ($this->longitude[0] == '.') {
    $this->longitude = '0' . $this->longitude;
}
Outputs: -.0989070

To simplify things, basically any . that has nothing before it, add in a 0, otherwise use the value given. I will need it for both longitude and latitude.

Upvotes: 0

Views: 210

Answers (6)

renshul
renshul

Reputation: 105

I created a class and assigned -.0982739 to a property called $longitude. In the constructor I did echo $this->longitude and it came out as -0.0982739 which I believe is exactly as you want it to be. I couldn't manage to reproduce it.

 <?php

class Test
{
    private $longitude = -.0982739;


    public function __construct()
    {
    }

    public function test()
    {
        echo $this->longitude;
    }
}



<?php
include "Test.php";

$test = new Test();
$test->test();

Upvotes: 0

Kevin Gales
Kevin Gales

Reputation: 532

You could check for numbers before dot....

<?php 

if (str_contains($this->longitude, '.')) {
           $beforedot = strstr($this->longitude,'.',true); //Get anything before
               if (is_numeric($beforedot)) { echo "do nothing";  }  //check if is number do nothing
                    else {   
                        $this->longitude = str_replace('.', '0.', $this->longitude); //else do replace . with 0.
                }
                                         };

?>

Upvotes: 0

kalide
kalide

Reputation: 99

`$var = 0.0989070;

$neg = -$var; // easiest $neg = -1 * $var; // bit more explicit $neg = 0 - $var; // another version`

Upvotes: 0

nice_dev
nice_dev

Reputation: 17805

/^([-+])?\./

The above regex matches the signs - and + if they are present and immediately followed by a .. Now, capture the matched group 1 in the regex which is ([-+])? and append 0. followed by all digits after . by taking substr of the current string.

<?php

$a = ".0982739";

if(preg_match('/^([-+])?\./',$a, $matches) === 1){
    $prefix = $matches[1] ?? '';
    $a = $prefix . '0.' . substr($a,$prefix == '' ? 1 : 2);
}

echo $a;

Upvotes: 2

PHP Guru
PHP Guru

Reputation: 1558

You could use str_replace:

$this->longitude = ltrim($this->longitude, '+');// remove leading +

if ($this->longitude[0]=='.' || substr($this->longitude, 0, 2)=='-.')) {
    $this->longitude = str_replace('.', '0.', $this->longitude);
}

The if condition matches any string that begins with '.' or '-.' And if so, str_replace replaces the . with 0.

Upvotes: 0

B Aerts
B Aerts

Reputation: 134

Try this :

    <?php

$x = -.54321 ;
echo $x . "\r\n" ;

$x = "-.12345" ;

echo $x . "\r\n" ;

echo floatval($x) . "\r\n" ;

echo sprintf("%0.5f", $x) ; 
?>

I assume your CSV is returning only string values, because as soon as I echo a "native" float, the echo is just fine.

As soon as your float is correctly formatted, you can catch it in a string value if needed.

Upvotes: 0

Related Questions