我正在使用Java字符串。我的問題是我有一串「1.500000154」。我如何從原始的「1.500000154」字符串中得到結果字符串「1.5」?如何從字符串中獲取「1.2」格式的字符串?
1.500000154 >>>>>>>> 1.5
我正在使用Java字符串。我的問題是我有一串「1.500000154」。我如何從原始的「1.500000154」字符串中得到結果字符串「1.5」?如何從字符串中獲取「1.2」格式的字符串?
1.500000154 >>>>>>>> 1.5
BigDecimal bigDecimal = new BigDecimal("1.500000154");
bigDecimal.setScale(1, BigDecimal.ROUND_HALF_UP)
String result - bigDecimal.toString();
謝謝你的回覆。我試着去做 – user1360797
這也爲我工作:
String str = "1.5000000154";
DecimalFormat df = new DecimalFormat("##.#");
System.out.println(df.format(Double.parseDouble(str)));
打印:
1.5
更多關於Decimal的信息格式可以獲得here。如果有需要的話,這也需要整理。例如,1.5900000154
,將導致1.6
。
你嘗試過什麼嗎? – Ewald