Thursday, August 11, 2022

[FIXED] How to add value to decimal places

Issue

I have some list of gps coordinates of latitude and longitude. How can I add random int value at the end of the float variable of latitude. I have tried this...

float log = 77.567635f;
Random rand = new Random();
int r = rand.Next(1, 20);

Let suppose value of r is 12, I would like to add to log '77.567635' + r.

My expecting result will be '77.567647'


Solution

What I would do is specify the precision of the floating point number first. If for example the precision is 0.000001 (like in your example) you can multiply the randomly generated number with this precision and add it to your coordinate.

float log = 77.567635f;
Random rand = new Random();
float r = rand.Next(1, 20) * 0.000001f;
log = log+r;


Answered By - wserr
Answer Checked By - Senaida (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.