[英]Java Object format to String Number with Decimals
所以我有一個對象數組列表,其中是字符串數字。 我想為這些數字添加小數位 (8)。
String value = String.valueOf(accountEntry.get(4));
double amount = Double.valueOf(value);
String formatted = String.format(Locale.GERMANY,"%.8f",amount);
accountEntry.add(formatted);
例如101700000000
應該輸出1017
但它是101700000000,00000000
有誰知道問題出在哪里?
有誰知道問題出在哪里?
您的輸入是: 101700000000 並且您正在格式化String.format(Locale.GERMANY,"%.8f",amount);
在這里你的輸出將是101700000000,00000000
所以為了理解這個字符串格式不會將你的輸入神奇地轉換為 1017。你需要使用另一種算法來解決這個問題
您好,使用 Regex 嘗試類似的操作,這樣您就可以在最后刪除所有零。
String value = String.valueOf("101700000000");
double amount = Double.valueOf(value);
String formatted = String.format(Locale.GERMANY,"%d",(long)amount);
formatted = formatted.replaceAll("0+$", "");
System.out.println(formatted);
輸入:101700000000 ===> 輸出:1017
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.