Adding 2 double variables together and get unsual results

I am trying to read a file and split the data up into some variables.

So far so good. I then want to generate a random number of between .009999 and .001000.

The idea is that I am generating some latitude/longitude data, but want to change the coordinates of the latitude and longitude - so I can create new records to push to an end point.

Using Write-Host I can see I had a valid Latitude and Longitude

Prechanges Lat : -39.682536 Prechanges Long :176.880903
Generate Additional Lat : 4.92699109433544E-05 Generate Additional Long : 6.55414916647326E-05
PostChanges Lat : -39.6825364.92699109433544E-05 PostChanges Long : 176.8809036.55414916647326E-05

The numbers are being appended - but both variables are double which have been checked by gettype(). My simple expectation was this would work : $lat1 = $lat1 + $addlat1

Where $lat1 was 39.682536, $addlat1 was 4.92699109433544E-05 - but the answer I am getting -39.6825364.92699109433544E-05

I am basically after a way to get a latitude and longitude and add a random small value to it - so it still sits in the rough geo location I am interested in.

I am new to powershell and would use another language if I could - but I need to try and get this to work. I am likely to be doing the wrong way - so need a few pointers.

I was using Get-Random to generate the random number and it seemed to do that OK

Any thoughts for a newbie.

ANDREW,
Welcome to the forum. :wave:t3:

Could you please share the exact code you used, the output you get and the expected result formatted as code?

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

If I use this code:

$lat1 = 39.682536
$addlat1 = 4.92699109433544E-05
$lat1 + $addlat1

I get:

39,6825852699109

I suppose that the root of your problem is in the following.
As far as I got, you

read a file and split the data up into some variables

So your variables are of the string type. In binary operators (with two operands) PoSh implicitly casts the right operand to a type of the left operand so that '5' + 10 results in 510 string value, while 10 + '5' results in 15 integer value. Just try it yourself in a PoSh command line. You should also concern of the fact when comparing values because the following code, for example, will lead to unexpected effects:

$a = Read-Host
$a -lt 10

The result will be False if a user inputs 5 cause Read-Host cmdlet by default treats inputs as strings and alphabetically the string ‘10’ appears before the string ‘5’. You should explicitly cast user input to integer type like [int]$a = Read-Host or just exchange the operand places: 10 -gt $a.
The same thing in your script: pay an attention on what of the values are on the first place in additions – the one from file (which is a string)? If so, put it after a plus sign and a random value (which is an integer) at the first place or explicitly cast your variables (which are from file) to an integer type by adding a [int] construct before the variable’s name.
Hope this would help!