我們可以看看convert
方法的源代碼
public static String convert(double coordinate, int outputType) {
if (coordinate < -180.0 || coordinate > 180.0 ||
Double.isNaN(coordinate)) {
throw new IllegalArgumentException("coordinate=" + coordinate);
}
if ((outputType != FORMAT_DEGREES) &&
(outputType != FORMAT_MINUTES) &&
(outputType != FORMAT_SECONDS)) {
throw new IllegalArgumentException("outputType=" + outputType);
}
StringBuilder sb = new StringBuilder();
// Handle negative values
if (coordinate < 0) {
sb.append('-');
coordinate = -coordinate;
}
DecimalFormat df = new DecimalFormat("###.#####");
if (outputType == FORMAT_MINUTES || outputType == FORMAT_SECONDS) {
int degrees = (int) Math.floor(coordinate);
sb.append(degrees);
sb.append(':');
coordinate -= degrees;
coordinate *= 60.0;
if (outputType == FORMAT_SECONDS) {
int minutes = (int) Math.floor(coordinate);
sb.append(minutes);
sb.append(':');
coordinate -= minutes;
coordinate *= 60.0;
}
}
sb.append(df.format(coordinate));
return sb.toString();
}
我們可以看到它使用與給定模式的DecimalFormat
得到它。所以,如果我們放眼DecimalFormat的構造函數:
public DecimalFormat(String pattern) {
// Always applyPattern after the symbols are set
this.symbols = new DecimalFormatSymbols(Locale.getDefault());
applyPattern(pattern, false);
}
我們可以在這裏看到,即使我們給一個模式,它使用的語言環境值。 Javadoc中也說:
參數:
模式非本地化的模式字符串。
要完成,我們就可以去這裏看到的數字代表不同的地方變體:http://docs.oracle.com/cd/E19455-01/806-0169/overview-9/index.html
所以我們可以看到,美國英語使用「點格式」和西班牙使用「逗號格式」 。
要回答你的問題:你面對的proflem可能是由於你的語言環境的十進制格式。我建議你在轉換對象類型以便對它們進行操作時非常小心。將int轉換爲String應該僅用於顯示它。
我認爲,當它劇照浮動(或任何小數類型),你應該獨立你的電話號碼的小數部分,那麼你的對象轉換爲字符串,以顯示它。你可以看看Math
類或搜索,以便獲取有關如何這一些示例;)
而且,@Dmitry說,你可以用DecimalFormatSymbols.getDecimalSeparator()
得到DecimalSeparator。
來源
Location.convert(double,int) source code
DecimalFormat(String) source code
Java "Decimal and thousands separators"
這實在是詳細的解答。感謝您鏈接到Oracle文檔,這非常有用。我不知道有些語言使用點作爲組分隔符 – Dmitry 2014-10-31 10:46:47