How to round decimal to the nearest fraction?

Not sure if this is the right way to ask about this or not, but here is the problem.

Given the latitude of 26.746346081599476, how can I find 26.75 as 16 greater than the number, and 26.6875 as the 16th number below the number?

26.0 26.0625 26.125 26.1875 26.25 26.3125 26.375 26.4375 26.5 26.5625 26.625 26.6875 My Number: 26.746346081599476 26.75 26.8125 26.875 26.9375 27.0 

I use JavaScript, so the answer will be useful, but not necessary. I could use it, but I'm looking for an elegant way to do it.

The larger the picture, the more I want to create standard tiles for the matching application I'm working on. We use Bing cards, and I download data on demand, every time the user clicks or zooms. It would be nice to use server-side caching for these requests, so if I standardized the requests sent to the server, I would get some cache hits. If I do not standardize server requests, it is unlikely that the same user will view the exact location at the same time.

Thus, it is possible to get caching with: /path/data.json?tl=26.6875,-80.6875&br=26.75,-80.75 than with: /path/data.json?tl=26.74946187679896,-80.10930061340332&br=26.7432342707028785857.09807.09807.09807.09807

Any external answers are also welcome.

+7
javascript decimal math gis bing-maps
source share
6 answers

To find the nearest multiple 1 / n:

 lower_bound = 1.0 / n * Math.floor(n * your_number); upper_bound = 1.0 / n * Math.ceil(n * your_number); 

You might want to use special processing if your number is already a multiple of 1/16.

 // alternate solution so that lower_bound <= your_number < upper_bound lower_bound = 1.0 / n * Math.floor(n * your_number); upper_bound = 1.0 / n * Math.floor(n * your_number + 1.0); 
+14
source share

You multiply the value by 16, use the floor or ceiling method and divide by 16:

 var higher = Math.ceil(number * 16) / 16; var lower = Math.floor(number * 16) / 16; 
+9
source share

Sounds like rounding to the nearest 16th ...

 rounded = Math.round(number * 16) / 16; 

You can get numbers that are not exact due to the float representation, but that doesn't matter in your case if you only use it for caching.

+1
source share
 function bounds(number, numerator, denominator) { var frac = denominator/numerator; return { lower: Math.floor(frac * number) / frac, upper: Math.ceil(frac * number) / frac, } } bounds(26.746346081599476,1,16) // returns an object with properties // lower : 26.6875 // upper : 26.75 
+1
source share

What are the smallest fractions that interest you? IE is the 16th place, which will be the smallest step?

If so, just multiply your number by 16. Translate it into int and divide by 16 to find the bottom border. Translate it to int, add 1, then divide by 16 to find the top border.

0
source share

A few strategies not yet published:

A) Create a lookup table that displays the numbers after the decimal point to the nearest 16th. Just use whatever accuracy you need (possibly hundredths).

B) create a table from all 16s from 0 to 1 and do a binary search with your number% 1.

0
source share

All Articles