PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 11, 2022

[FIXED] How to add value to decimal places

 August 11, 2022     add, c#, decimal     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing