2013-03-19 44 views
-2

如何设置输出格式latlng像这样:0.000000设置双重格式

double lat = marker.getPosition().latitude; 
    double lng = marker.getPosition().longitude; 

优选不转化为String,使得输出是一个double

+4

你的问题没有道理 - 变量没有输出格式。如果您只想输出第一个X位数字,那么您需要将其转换为字符串。 – 2013-03-19 20:52:09

+0

[This](http://developer.android.com/reference/java/text/DecimalFormat.html)可以帮助你。 – TronicZomB 2013-03-19 20:54:04

回答

1

Java中没有任何数字与它们有关联的输出格式。要输出它们,即使您拨打System.out.println(marker.getPosition().latitude),它们也会转换为String

与任何数字一样,可以格式化double,但仅在转换为String时才可以。

您可以使用DecimalFormat

DecimalFormat df = new DecimalFormat("0.000000"); 
String formattedLat = df.format(marker.getPosition().latitude); 

(该0是必要的,而不是#,使尾随零显示出来。)

也可以使用String.format()

但是,如果要格式化数字,则需要转换为String

0

你可以试试这个方法使用DecimalFormat

double roundDecimals(double d) { 
    DecimalFormat twoDForm = new DecimalFormat("0.000000"); 
    return Double.valueOf(twoDForm.format(d)); 
} 

使用像四舍五入到小数点后6位:

double lat = roundDecimals(marker.getPosition().latitude); 
double lng = roundDecimals(marker.getPosition().longitude); 

发表@TronicZomB的链接有更多的相关信息,是很好看的。