0

I want to display GPS coordinates upto just 6 decimal places. e.g. if my GPS location is something like x.yyyyyyyyyy I want to display just x.yyyyyy and for this I use DecimalFormat class. But, if the number is like 8.3456709012, the output for the following code is like 8.34568

_yPos = 8.3456709012;
DecimalFormat decimalFormat = new DecimalFormat("#.######");
String yCoord = decimalFormat.format(_yPos);

whereas the expected output is 8.345670. Can anybody show me a way to do this?

4

3 に答える 3

3

"0.000000"フォーマットコードとして使用します。

文字#は「数字、ゼロは不在として表示」を表します:http: //docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html

また、出力が本当に必要なDOWN場合は、丸めモードを次のように設定する必要があります。8.3456708.345671

DecimalFormat decimalFormat = new DecimalFormat("0.000000");
decimalFormat.setRoundingMode(java.math.RoundingMode.DOWN);
于 2012-04-24T07:12:11.410 に答える
2

Joni Salonen's answer suggests how you can preserve trailing zeros, but the coordinates will still be half rounded as the default rounding mode is RoundingMode.HALF_EVEN.

You can set the rounding mode by using

decimalFormat.setRoundingMode(RoundingMode.DOWN);

This method is available since 1.6

Refer http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html#setRoundingMode%28java.math.RoundingMode%29

于 2012-04-24T07:14:42.170 に答える
1

新しいDecimalFormat( "#。##### 0")ですか?

http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html

于 2012-04-24T07:11:58.913 に答える