This is my second post on this subject. I have found dozens of posts on this subject and not a single solution that works. The location listener will provide you a latitude such as 3.734567E7. My goal is to reduce the accuracy of that latitude to four decimal places. I don't care if I truncate it or round it. the following code...
Double d = Double.parseDouble("3.734567E7"); NumberFormat fmtr = new DecimalFormat("###.####"); String f = fmtr.format(d); Log.w(getClass().getName(), "result using DecimalFormat = " + f ); d = (double) Math.round(d * 10000.0) / 10000.0; Log.w(getClass().getName(), "result using math round = " + d ); f = d.toString(); Log.w(getClass().getName(), "result string using math round = " + f );
produces three lines: 37345670 3.734567E7 3.734567E7
none of this processing has any affect on the number.
when I had to do this in C# on my windows phone app I did the following...
myExecution.latitude = Math.Round(newLatitude, 4); It took less than a minute of my time. I have over a weeks time trying to figure out how to do the same thing in java. the Java math.round, oddley enough, has no way to specify how many decimal places you desire to round to making it totally usless as far as I can understand.
I realize that scientific notation is only supposed to be a way of displaying values and does not have anything to do with how an amount is stored internally. This makes this problem all the more perplexing.
Can anyone modify the code above to make it round to 4 decimal places?
(but you can't change the 1st line of code. It must be specified in scientific notation. If you specify it in decimal notation it works wonderfully. thanks, Gary
3.734567E7 means 3.734567 x 107 which is 37,345,670. So there are no decimals to round...
If you want 3.734600E7, you can use
Math.round(d / 1000) * 1000).