-2

For some reason I'm getting extra numbers on the end when I try to do this:

float lat = floor(1000.0 * origin.latitude) / 1000.0;

result: lat: 37.330002

Even converting the number to an int, and then back to a float, doesn't get rid of the numbers right on the end.

Andrew
  • 7,693
  • 11
  • 43
  • 81
  • Wouldn't multiplying and dividing by 1000 round to 3 decimal places, not 2? 37.333002 * 1000 = 37333.002. Floor that to get 37333. Divide by 1000 to get 37.333. – KevenDenen Apr 23 '14 at 17:31
  • This SO question may help you out some: http://stackoverflow.com/questions/1343890/rounding-number-to-2-decimal-places-in-c – Matt Becker Apr 23 '14 at 17:35

1 Answers1

2

Try this :

float lat = floorf(1000.0 * origin.latitude) / 1000.0;

In floor() is only for C++, C or Objective C use floorf() and multiplying and dividing by 1000 will give the three decimal point of any number.

Usman Awan
  • 1,208
  • 2
  • 13
  • 30