[英]Java string.format() only half working
我正在嘗試使用string.format()構造一個字符串,但是它在一半時間內停止工作(剛剛結束)。
private String constructMessage(String messageType, int messageCode,Position location) {
double lat = location.getLat();
double lon = location.getLon();
double ele = location.getEle();
double speed = 0.0;
double heading = 0.0;
String msg = "$PNGVEST," + messageType + "," + VEST_ID + ","
+ System.currentTimeMillis() + "," + lat + "," + lon + "," + ele
+ "," + speed + "," + heading + "," + messageCode;
String test = String.format("$PNGVEST,%s,%d,%d,%f,%f,%f,$f,$f,$d",
messageType, VEST_ID, System.currentTimeMillis(), lat, lon, ele,
speed, heading, messageCode);
System.out.println(msg);
System.out.println(test);
return msg;
}
當我運行上面的代碼時,得到以下輸出:
$PNGVEST,LOCATION,1,1439998967553,51.558641765246364,-3.043043462354341,-1.0,0.0,0.0,0
$PNGVEST,LOCATION,1,1439998967553,51.558642,-3.043043,-1.000000,$f,$f,$d
$PNGVEST,LOCATION,1,1439998968553,51.55862560874394,-3.043027770590568,-1.0,0.0,0.0,0
$PNGVEST,LOCATION,1,1439998968553,51.558626,-3.043028,-1.000000,$f,$f,$d
為什么不格式化最后三個變量?
您使用最后3個變量進行了拼寫錯誤。 您注意到$f
和$d
而不是%f
和%d
。
它應該是 :
String test = String.format("$PNGVEST,%s,%d,%d,%f,%f,%f,%f,%f,%d",
messageType, VEST_ID, System.currentTimeMillis(), lat, lon, ele,
speed, heading, messageCode);
代替
String.format("$PNGVEST,%s,%d,%d,%f,%f,%f,$f,$f,$d", ...);
嘗試改變
String test = String.format("$PNGVEST,%s,%d,%d,%f,%f,%f,$f,$f,$d",
messageType, VEST_ID, System.currentTimeMillis(), lat, lon, ele,
speed, heading, messageCode);
至
String test = String.format("$PNGVEST,%s,%d,%d,%f,%f,%f,%f,%f,%d",
messageType, VEST_ID, System.currentTimeMillis(), lat, lon, ele,
speed, heading, messageCode);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.